Python code implementation of the disk spam cleaner,
This article assumes that some specific types of files and files with a size of 0 are spam files, you can freely expand the list of code, that is, the type of spam files.
From OS. path import isdir, join, splitextfrom OS import remove, listdir, chmod, statimport sys # specify the file type to be deleted filetypes = ['. tmp ','. log ','. obj ', '.txt'] def delCertainFiles (directory): for filename in listdir (directory): temp = join (directory, filename) if isdir (temp ): # recursively call delCertainFiles (temp) elif splitext (temp) [1] in filetypes or stat (temp ). st_size = 0: # modify the file attributes and obtain the permission chmod (temp, 0o777) # delete the file remove (temp) print (temp, 'deleted .... ') if _ name _ =' _ main _ ': paths = sys. argv [1:] for path in paths: if isdir (path): delCertainFiles (path)
Save the above Code as t. py, then open the Command Prompt window and execute the command "Python t. py c: \ test, where "c: \ test" indicates the folder to be cleaned up. If there are multiple folders to be cleaned up, use spaces to separate them.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.