How Python deletes Windows junk files

Source: Internet
Author: User
The example in this article describes how Python deletes Windows junk files. Share to everyone for your reference. Specific as follows:

#coding: Utf-8import os#from glob import globif os.name = ' nt ': If ' homepath ' in os.environ:home = os.environ[' homedrive ' ] + os.environ[' homepath ' else:home = os.environ[' HomePath ']workpath = Os.path.join (home, ' Local Settings ') #递归删除文件 # The inside and below function with try is thrown to delete the file that is being used. Error def delfile (path): For file in Os.listdir (path): If Os.path.isfile (Os.path.join (path,file ): Try:print "\ n Delete junk file:%s"% (Os.path.join (path,file)) Os.remove (Os.path.join (path,file)) Except:pass elif Os.pat H.isdir (Os.path.join (path,file)): Delfile (Os.path.join (path,file)) Else:passdelfile (Os.path.join (Workpath, ' Temp ') ) Delfile (Os.path.join (Workpath, ' temporary Internet Files ') #删除文件家的时候必须为空文件夹, and can only be deleted from the innermost def Deldir (PA): For I in Os.listdir (PA): If Os.path.isdir (Os.path.join (pa,i)): If Len (Os.listdir (os.path.join)) > Pa,i ( Os.path.join (Pa,i)) Try:os.rmdir (Os.path.join (pa,i)) Except:pass else:try:print "\ n Delete folder%s"% (Os.path.join   (Pa,i)) Os.rmdir (Os.path.join (pa,i)) Except:passdeldir (oS.path.join (Workpath, ' Temp ')) Deldir (Os.path.join (Workpath, ' temporary Internet Files ') print "" "system generated by zero garbage files cleaned up! "" Raw_input ("please press ENTER to exit!") ")

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.