#encoding: utf-8#author: walker#date: 2017-06-15#summary: Custom Folder handler function for Python3.5+import osimport shutilimport win32com.client# emptying directory Def cleardir (dir): print (' cleardir ' + dir + ' ... ') for entry in os.scandir (dir): if Entry.name.startswith ('. '): continue if entry.is_file (): os.remove (entry.path) #删除文件 else: shutil.rmtree (entrY.path) #删除目录 # Get directory Size # not present or empty directory returns 0def getdirsize (pathdir): if not os.path.exists (pathdir ): print (' warning: not exists %s ' % Pathdir) return 0 fso = Win32com.client.Dispatch (' Scripting.FileSystemObject ') folder = fso. GetFolder (pathdir) return folder. size ' # Merge source directory to destination directory, empty directory in source directory will not be processed # src_dir: source directory # dst_dir: Whether the destination directory # reserve_src: retains the source data # override: whether to overwrite the files in the destination directory "' def mergedir (src_root, dst_ root, reserve_src=true, override=true): if (not os.path.exists (src_ Root) or (not os.path.exists (dst_root)): #目录不存在 raise FileNotFoundError for parent, dirnames, filenames in os.walk (src_root): for filename in filenames: src_file = os.path.join (parent, filename) dst_file = os.path.join (dst_root, src_ File[len (src_root) + 1:]) if Os.path.exists (dst_file) and (not override): #如果目标文件存在且不能被覆盖 continue dst_dir = os.path.dirname (dst_file) if not os.path.exists (dst_dir): os.makedirs (dst_dir) if reserve_src : #保留源数据 shutil.copyfile (src_file, dst_file) #会覆盖目标文件 else: shutil.move (src_file, dst_file) if not reserve_src: shutil.rmtree (src_root) #删除源根目录
Related Links:
1, pywin32 Download
2. Python file (clip) basic operation
Walker * * *
This article is from "walker's journal" blog, Please be sure to keep this source http://walkerqt.blog.51cto.com/1310630/1936982
Custom Folder handler function (Python)