How to delete windows junk files using Python

Source: Internet
Author: User
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.

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.