Custom Folder handler function (Python)

Source: Internet
Author: User

#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.