Python deletes the source code of an empty folder,

Source: Internet
Author: User

Python deletes the source code of an empty folder,

Although empty folders do not occupy space, they sometimes seem annoying (don't misunderstand, I'm not obsessive !), So I wrote a small program to delete the empty folder in the current directory.

 

Environment: win7 64-bit; python2.7; IDE pycharm2016.1

 

The specific implementation is as follows:

#! /Usr/bin/env python # coding = utf-8import OS, timedef del_emfile (path = OS. getcwd (): ''' parameter: path defaults to the current path ''' folder_num = 0 efile = [] for I in OS. walk (path): if len (I [1]) = 0 and len (I [2]) = 0: efile. append (I [0]) print 'empty Folder \ t', I [0] folder_num + = 1 print 'empty Folder \ t ', folder_num raw_input ('\ n press any key to delete · \ n') efile. reverse () for f in efile: try: # OS. rmdir (f) print 'delete \ t', f then t: print 'Delete \ t', f,' \ t failed 'print '\ n Delete empty folder \ t ', folder_num, '\ n' return folder_numif _ name _ =' _ main _ ': d = 1 while d: d = del_emfile () time. sleep (100)

Result:

This source code may not be perfect. You can use this template for optimization.

 

Note: the source code is purely original. for academic purposes, please indicate the source. for commercial purposes, please contact me first.

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.