In this paper, we assume that certain specific types of files and files of size 0 are garbage files, and we can freely expand the list of code, which is the type of garbage file.
from import isdir, join, splitext from os import remove, listdir, chmod, stat import sys # Specify the type of file to be deleted filetypes = ['.tmp', '.log', '.obj', '.txt'] def delCertainFiles(directory): for filename in listdir(directory): temp = join(directory, filename) if isdir(temp): # Recursive calls delCertainFiles(temp) elif splitext(temp)[1] in filetypes or stat(temp).st_size==0: # Modify file attributes to gain access chmod(temp, 0o777) # Delete the file remove(temp) print(temp, ' deleted....') if __name__ == '__main__': paths = [1:] for path in paths: if isdir(path): delCertainFiles(path)
Save the above code as, and then open a command prompt window, execute the command "Python c:\test", where "c:\test" represents the folder to be cleaned, if there are multiple folders to be cleaned, you can use the If you have more than one folder to clean, you can use spaces to separate them.
This is the whole content of this article, I hope it will help you to learn more.