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.