Last active
August 29, 2015 14:19
-
-
Save axiaoxin/b41386858b693a270acd to your computer and use it in GitHub Desktop.
clear windows dirs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python | |
| # -*- coding:utf-8 -*- | |
| import os | |
| import time | |
| def clear(path): | |
| for file_or_path in os.listdir(path): | |
| file_name = os.path.join(path, file_or_path) | |
| if os.path.isfile(file_name): | |
| try: | |
| os.remove(file_name) | |
| print "--- removed", file_name | |
| except Exception, e: | |
| print "*** can't delete", file_name | |
| elif os.path.isdir(file_name): | |
| clear(file_name) | |
| try: | |
| os.rmdir(file_name) | |
| print "=== removed", file_name | |
| except Exception, e: | |
| print "*** can't delete", file_name | |
| def clear_dirs(paths): | |
| for path in paths: | |
| if not os.path.exists(path): | |
| continue | |
| else: | |
| clear(path) | |
| def delete_dirs(root): | |
| for path, subdirs, files in os.walk(root): | |
| if not subdirs and not files: | |
| try: | |
| os.rmdir(path) | |
| print "*remove", path | |
| except: | |
| pass | |
| else: | |
| for subdir in subdirs: | |
| new_path = os.path.join(path, subdir) | |
| print new_path | |
| delete_dirs(new_path) | |
| def delete_empty_dirs(root_dirs): | |
| for root in root_dirs: | |
| delete_dirs(root) | |
| def main(): | |
| paths = [ | |
| "C:/Windows/Temp", | |
| "C:/Users/AshIn/AppData/Local/Temp", | |
| "C:/ProgramData/TEMP", | |
| "C:/Users/AshIn/AppData/Local/Microsoft/Windows/Caches/", | |
| "C:/Users/AshIn/AppData/Local/Microsoft/Media Player/Cache1634469", | |
| "C:/Users/AshIn/AppData/Local/Microsoft Games/", | |
| "C:/Users/AshIn/AppData/Local/TechSmith/Snagit/DataStore/", | |
| "C:/Users/AshIn/AppData/Local/VisualAssist/", | |
| "C:/Users/AshIn/AppData/Local/Yodao/DeskDict/CrashRpt/", | |
| "C:/Users/AshIn/AppData/Roaming/Tencent/Logs", | |
| "C:/Users/AshIn/AppData/Local/Google/Chrome/User Data/Default/Cache", | |
| "C:/Users/AshIn/AppData/Local/Google/Chrome/User Data/Default/Media Cache", | |
| ] | |
| root_dirs = [ | |
| "C:/Program Files", | |
| "C:/ProgramData", | |
| "C:/Users", | |
| ] | |
| start_time = time.time() | |
| clear_dirs(paths) | |
| # delete_empty_dirs(root_dirs) | |
| end_time = time.time() | |
| use_time = end_time - start_time | |
| print "-*- over use {time}s -*-".format(time=use_time,) | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| #-*- coding:utf-8 -*- | |
| __author__ = 'ashin' | |
| import os | |
| import stat | |
| def del_files(path): | |
| ''' | |
| 清空.svn文件夹内所有文件,并删除清空后的空文件夹 | |
| ''' | |
| files = os.listdir(path) | |
| for f in files: | |
| f_path = os.path.join(path, f) | |
| if os.path.isfile(f_path): | |
| os.chmod(f_path, stat.S_IWRITE) # 有些只读文件没法删,要修改权限 | |
| os.remove(f_path) | |
| print '---removed %s' % f_path | |
| elif os.path.isdir(f_path): | |
| # 递归清空文件夹并删除 | |
| del_files(f_path) | |
| os.rmdir(f_path) | |
| def clear_svn(path): | |
| ''' | |
| 清空给定路径下的所有.svn文件夹 | |
| ''' | |
| for path, subdirs, files in os.walk(path): | |
| if '.svn' in subdirs: | |
| svn_path = os.path.join(path, '.svn') | |
| del_files(svn_path) | |
| try: | |
| # 会首先删除根目录的.svn,但是此时不是空文件夹,无法删除 | |
| os.rmdir(svn_path) | |
| print 'removed %s' % svn_path | |
| except: | |
| pass | |
| if __name__ == '__main__': | |
| clear_svn('.') # 清空当前目录下所有.svn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment