Python code implementation of the disk spam cleaner,

Source: Internet
Author: User

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.

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.