Skip to content

Instantly share code, notes, and snippets.

@lixingcong
Created February 14, 2018 14:16
Show Gist options
  • Save lixingcong/d125a441ff54bbeb20a3618068242d65 to your computer and use it in GitHub Desktop.
Save lixingcong/d125a441ff54bbeb20a3618068242d65 to your computer and use it in GitHub Desktop.
kindle delete useless sdr files
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Create on: 2015年10月25日 下午4:48:30
# 说明:删除重复文件 dfs递归搜索目录判断有垃圾sdr文件
# Author:lixingcong
import os
import shutil
import time
# Generator of un-duplicated files
def gen_a_dup_list(sdr_, normal_file_):
for sdr in sdr_:
sdr_cut = sdr[:-4]
if sdr_cut not in normal_file_:
yield sdr
# Super remove not-empty files
def rm_dir(path, count):
try:
shutil.rmtree(path)
print "#%d Deleted: %s" % (count, path)
except:
print "#%d ERROR deleting dir: %s" % (count, path)
# Delete all useless sdr files
def main_delete(path):
print '== ==' * 10
print "Now Scan for trash in this folder:", path
sdr = []
normal_files = []
sub_dirs = []
list_dir = os.listdir(path)
for t in list_dir:
# Collect sdr files
if t.endswith('.sdr'):
sdr.append(t)
# print t[:-4]
# Collect book files
elif os.path.isfile(path + t):
len_of_filename = len(t)
for rev_x in xrange(2, len_of_filename):
# extand name will be removed
if t[-rev_x] == '.':
t1 = t[0:-rev_x]
normal_files.append(t1)
break
# Collect sub-folders
elif os.path.isdir(path + t):
sub_dirs.append(t)
sdr_to_del = gen_a_dup_list(sdr, normal_files)
# To check if the generator is empty, you should change it to list
# If you have not changed, you should use method next() then throw a exception
del_list = list(sdr_to_del)
if del_list == []:
print "Yes, Sir. There is no useless sdr!"
else:
count = 1
for sdr_ in del_list:
rm_dir(path + sdr_, count)
count = count + 1
# Recusive delete
if sub_dirs != []:
for sub_dirs_ in sub_dirs:
# You should not forget to add '/' as its tail
# to prevent some serious problem in windows
main_delete(path + sub_dirs_ + '/')
if __name__ == "__main__":
start_time=time.time()
# You should not forget to add '/' as its tail
main_delete('./documents/')
end_time=time.time()
print '== =='*10
print 'Time elapsed: %.2fs'%(end_time-start_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment