How to delete Empty files and folders in Python

Source: Internet
Author: User
This article describes how to delete Empty files and folders in Python. It involves Python's skills in file and folder traversal, judgment, and deletion. It has some reference value, for more information about how to delete Empty files and folders in Python, see the following example. Share it with you for your reference. The specific implementation method is as follows:

#-*-Coding: cp936-*-"" OS. walk () function declaration: walk (top, topdown = True, onerror = None) 1> the top parameter indicates the path of the directory tree to be traversed. 2> the default value of the topdown parameter is "True", indicating that the files under the directory tree are first returned, and then the subdirectories of the directory tree are traversed. if the value of Topdown is "False", the system first traverses the subdirectories of the directory tree and returns the files in the subdirectories, finally, the system returns the file in the root directory. 3> the default onerror parameter is "None", indicating that the errors generated by file traversal are ignored. if it is not empty, a custom function is provided to indicate an error message, and then traverse continues or throw an exception to abort traversal. 4> This function returns a tuple with three elements, the three elements represent the path name for each traversal. A new partition function is added to the directory list and file list in version 2.5. What can this function do? For example, >>>' http://www.donews.net/limodou '. Partition (': //') ('HTTP ',': // ', 'www .donews.net/limodou') >>> 'file:/a.html '. partition (': //') ('file:/a.html ', ",") from the first example, we can see that it is used to split strings based on specified delimiters, if the string contains the specified separator, a 3-element tuple is returned. The first is the substring on the left of the separator, the second is the separator itself, and the third is the substring on the right of the separator. The second example shows that if the specified separator is not found, a tuple of 3 RMB is returned, the first is the entire string, and 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: # scan the file list. try: fn = OS. path. join (root, file) # concatenate the path name and file name. size = OS. path. getsize (fn) # Get the file name size if size! = 0: B = root while (B! = Path): a [B] = 1 B = B. rpartition ("\") [0] # Save the name of the upper-level directory a [path] = 1 else: try: OS. remove (fn) # delete a file with an empty file name a [root] = 0 tb t (WindowsError): B = root while (B! = Path): a [B] = 1 B = B. rpartition ("\") [0] a [path] = 1 character t 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. 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) # Delete directory print "% s deleted !! "% (I) Partition T (WindowsError): print" % s cannot be deleted !! "% (I) if _ name _ =" _ main _ ": remove_empty_dir ('f: \ B ')

I hope 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.