Python methods for deleting empty files and empty folders

Source: Internet
Author: User
The example in this article describes how Python deletes empty files and empty folders. Share to everyone for your reference. The implementation method is as follows:

#-*-coding:cp936-*-"" "Os.walk () function declaration: Walk (Top,topdown=true,onerror=none) 1> parameter top indicates the path of the directory tree that needs to be traversed 2> The default value of the parameter topdown is "True", which means that the files under the directory tree are returned first, and then the subdirectories of the directory tree are traversed. When the value of Topdown is "False", it means traversing the subdirectory of the directory tree, returning the file under the subdirectory, and finally returning the file under the root directory 3> the default value of OnError is "None", Represents an error that occurs when a file traversal is ignored. If not NULL, provide a custom function tip error message after continuing to traverse or throw an exception to abort traversal 4> the function returns a tuple that has 3 elements, each of which represents the path name of each traversal, The list of directories and files adds a new function called partition in version 2.5, what can it do? Give a small example:>>> ' Http://www.donews.net/limodou '. Partition ('://') (' http ', '://', ' Www.donews.net/limodou ') > >> ' file:/a.html '. Partition ('://') (' file:/a.html ', ",") as can be seen from the first example, it is used to split the string according to the specified delimiter, if the string contains the specified delimiter, Returns a tuple of $3, the first is the substring to the left of the delimiter, the second is the delimiter itself, and the third is the substring to the right of the delimiter. The second example shows that if the specified delimiter is not found, the return is still a tuple of $3, the first is the entire string, the second and third are empty strings "" "Import OS, sysdef Remove_empty_dir (path): Print path while (path[-1] = = "\"): Path = path[:-1] Print path a = {} for root, dirs, files in Os.walk (path, False): If len (files) = = 0:a[root] = 0 else:for file in Files: #对文件列表进行扫描 Try:fn = os.path.join (root, file) #将路径名和文件名拼接起来 size = OS.PA Th.getsizE (FN) #获取文件名大小 if size! = 0:b = root while (b! = path): a[b] = 1 B = b.rpartition ("\ \") [0] #保存上一级目录名 A[path]     = 1 Else:try:os.remove (FN) #删除文件名为空的文件 A[root] = 0 except (windowserror): b = root while (b! = path):     A[B] = 1 B = b.rpartition ("\ \") [0] A[path] = 1 except windowserror:b = root while (b! = path): a[b] = 1 b = b.rpartition ("\ \") [0] A[path] = 1 if a[root]: break; Empty_dirs = [] for I, J in A.iteritems (): if j = = 0:print I empty_dirs.insert (0, i) del a empty_dirs.sort (reverse=true ) for I in Empty_dirs:try:os.rmdir (i) #删除目录 print "%s deleted!!"% (i) except (windowserror): print "%s deleted!!"% (i) if __name__ = = "__main__": Remove_empty_dir (' f:\\b ')

Hopefully this article will help you with Python programming.

  • 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.