Use the python program to clean up the windows garbage
- 2020-05-19 05:12:56
- OfStack
preface
As you may know, some "junk" files will be generated after using windows system for a long time. Some of these files are temporary files of the program, some are operation records or logs, etc. The accumulation of garbage over time leads to the reduction of available space and excessive file fragmentation, which affects the running speed of the system.
The Mac and Linux systems do not have such problems, so they only apply to windows
Knowledge summary
Some cache files can improve the execution speed of the program, such as caching cookie, using record recent, prereading prefetch, and so on. So cleaning up temporary files doesn't mean your system will run faster, and sometimes slower.
What are the main junk files and folders on windows computers?
System disk %system%
[temporary document (*.tmp)]
[temporary file (*._mp)]
[log file (*.log)]
[temporary help document (*.gid)]
[disk check file (*.chk)]
[temporary backup file (*.old)]
[Excel backup file (*.xlk)]
[temporary backup file (*.bak)]
User directory %userprofile% under folder
cookies COOKIE 】 【 \ *. *
【 document usage record 】 recent\*.*
【IE temporary document 】 Temporary Internet Files\*.*
【 temporary file folder 】 Temp\*.*
Windows directory %windir% under folder
【 preread data folder 】 prefetch\*.*
[temporary document] temp\*.*
Get file address
Operation requires the os module, such as getting the working directory:
import os
print os.getcwd() # 'E:\\PythonSource\\leanr_py'
Switch working directory:
os.chdir('d://wamp')
print os.getcwd() # 'd:\\wamp'
Get system disk character:
os.environ['systemdrive'] # 'C:'
Get user directory:
os.environ['userprofile'] # 'C:\\Users\\Administrator'
Get Windows directory:
os.environ['windir'] # 'C:\\Windows'
Directory traversal
To traverse a folder, you need to use it
os.walk(top,topdown=True,onerror=None)
for roots, dirs, files in os.walk('d://wamp', topdown=False)
# roots Folder path, dirs List of folders in this directory, files File list
print roots # d://wamp
print dirs # ['bin', 'www', 'alias']
print files # ['wampmanage.conf', '1.txt']
Determine if the file is garbage
os.path.splitext()
You can cut file names
extension = os.path.splitext(r'aaa\bbb\ccc.ddd') # ('aaa\\bbb\\ccc', '.ddd')
if extension[1] in ['.tmp', '.bak']:
print ' It's a junk file '
Delete the file
Deleting files and deleting folders call different functions.
# Delete the file
os.remove('d:temporary/test/test.txt')
# Delete folder
os.rmdir('d:temporary/test/empty')
os.rmdir can only delete empty folders; if the folder is not empty, an error will be reported. So you should use:
shutil.rmtree('d:/dir1/dir2/aaa')
When the file is running or protected and the current account does not have sufficient permissions, an error is reported.
Finally, the collation and deletion function is:
def del_dir_or_file(root):
try:
if os.path.isfile(root):
# Delete the file
os.remove(root)
print 'file: ' + root + ' removed'
elif os.path.isdir(root):
# Delete folder
shutil.rmtree(root)
print 'directory: ' + root + ' removed'
except WindowsError:
print 'failure: ' + root + " can't remove"
Get file size
os.chdir('d://wamp')
print os.getcwd() # 'd:\\wamp'
0
Complete the program
Note: because the file deletion operation is involved, please be sure to confirm the code again and again before starting work.
Be sure to confirm!!
Be sure to confirm!!
Be sure to confirm!!
os.chdir('d://wamp')
print os.getcwd() # 'd:\\wamp'
1
conclusion
Recently I have been looking at some content of qt interface. Can be combined to do a graphical interface program. The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.