From c438ae5780c0793292445cb5ee6addf55cb8fe61 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 3 Aug 2019 21:56:49 -0500 Subject: [PATCH] move into proper function, add recycle capability --- clean.py | 61 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/clean.py b/clean.py index e0898e7..0634919 100644 --- a/clean.py +++ b/clean.py @@ -1,26 +1,43 @@ -import os, sys +import os, sys, send2trash -basepath = sys.path[0] blacklist = [ - 'clean.py', - 'main.py', - 'LICENSE', - 'output.jpg', - 'screenshot.png', - 'README.md', - '.git', - '.gitignore', + 'clean.py', + 'main.py', + 'LICENSE', + 'output.jpg', + 'screenshot.png', + 'README.md', + '.git', + '.gitignore', ] -_log = [] -print('DELETED SEQUENCE STARTED') -for file in os.listdir(basepath): - filepath = os.path.join(basepath, file) - if file not in blacklist: - _log.append('DELETED: \'{}\''.format(filepath)) - os.remove(filepath) - else: - pass - # print('IGNORED: \'{}\''.format(filepath)) -print('\n'.join(_log)) -print(f'FINISHED DELETED {len(_log)} FILES') \ No newline at end of file +def run(recycle=True, print_ignores=False): + """Main driver function for recycling unneeded files from the folder. + + Keyword Arguments: + recycle {bool} -- True to recycle and send to recycle bin instead of permanently delete a file. (default: {True}) + print_ignores {bool} -- True to print what files have been ignored (default: {False}) + """ + + # Constants + basepath = sys.path[0] + _log = [] + + print('Deletion sequence started') + for file in os.listdir(basepath): + filepath = os.path.join(basepath, file) + if file not in blacklist: + _log.append('Recycled: \'{}\''.format(filepath)) + # Send to recycle bin vs straightup permanently delete + if recycle: + send2trash.send2trash(filepath) + else: + os.remove(filepath) + else: + if print_ignores: + _log.append('IGNORED: \'{}\''.format(filepath)) + print('\n'.join(_log), end='\n' if _log else '') + print(f'Finished deleting {len(_log)} files') + +if __name__ == "__main__": + run() \ No newline at end of file