-
Notifications
You must be signed in to change notification settings - Fork 10
/
kindleCleaner.py
57 lines (47 loc) · 1.61 KB
/
kindleCleaner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# python3
import sys
import os
import shutil
from lib.kindlelib import findKindleDisk
includeExts = ['.mobi', '.azw', '.azw3', '.azw4', '.pdf', '.txt', '.prc', '.pobi', '.epub']
excludeDirs = ['dictionaries']
stdout_encoding = sys.stdout.encoding
def clean(workDir):
outDirs = []
count = 0
for root, dirs, files in os.walk(workDir):
# 取得所有存在的电子书名称(noExt)
allFiles = []
for file in files:
fileName, ext = os.path.splitext(file)
if ext in includeExts:
allFiles.append(fileName)
for dir in dirs:
if not '.sdr' in dir:
continue
if dir in excludeDirs:
continue
if dir[:-4] in allFiles: # 检验是否存在
continue
# 删除文件夹(可能非空)
dirPath = os.path.join(root, dir)
shutil.rmtree(dirPath)
# 当路径存在 gbk 不支持的字符时,print 就会错误
print('removed: %s' % dirPath.encode(stdout_encoding, 'replace').decode(stdout_encoding))
count += 1
# # 删除空文件夹
# if '.sdr' in dir:
# continue
# if not os.listdir(dirPath):
# os.rmdir(dirPath)
if count:
print('共删除 %s 个不存在的 sdr 文件夹' % count)
else:
print('没有需要删除的 sdr 文件夹')
def main():
disk = findKindleDisk()
if disk:
documentDir = os.path.join(disk, 'documents')
clean(documentDir)
if __name__ == '__main__':
main()