This article mainly introduces how to delete windows junk files by using Python, which involves how to search for and clean up System junk files. It has some reference value, for more information about how to delete windows junk files, see the following example. Share it with you for your reference. The details are 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') # recursively delete a file # Use try in the following functions to throw the following error def delfile (path) When deleting a file in use ): for file in OS. listdir (path): if OS. path. isfile (OS. path. join (path, file): try: print "\ n Delete the spam file: % s" % (OS. path. join (Path, file) OS. remove (OS. path. join (path, file) before T: pass elif OS. path. 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') # When deleting a file, the folder must be empty, and def deldir (pa): for I in OS can only be deleted from the innermost layer. listdir (pa): if OS. path. isdir (OS. path. join (pa, I): if len (OS. listdir (OS. path. join (pa, I)> 0: deldir (OS. path. join (pa, I) try: OS. rmdir (OS. path. join (pa, I) failed T: pass else: try: print "\ n Delete folder % s" % (OS. path. join (pa, I) OS. rmdir (OS. path. join (pa, I) Partition T: passdeldir (OS. path. join (workpath, 'temp ') deldir (OS. path. join (workpath, 'temporary Internet files') print "The system has cleared the junk Files at zero time! "Raw_input (" press enter to exit! ")
I hope this article will help you with Python programming.