Automatically clean up system garbage with Python, no more 360 security guards
Windows in the installation and use of the process will produce a considerable number of junk files, including temporary files (such as:TMP,._mp) log file (.log), temporary help files (.gid), disk check files (.chk), temporary backup files (for example:.old, *.bak) and other temporary files. In particular, if the Temporary Internet Files folder of Internet Explorer is not cleared for a period of time, cache Files in the folder may occupy hundreds of MB of disk space. Not only do these LJ files waste valuable disk space, but in severe cases they can slow your system to a crawl.Clean up the computer garbage is the most commonly used 360 security guards, looking at the clean up completed, released N GB of space, do not mention more comfortable. But as Pythoneer naturally wants to play a little bit different, I’m going to show you a way to automatically clean up your computer using a py script with a task plan, if you’re interested.
Students don’t be too serious ha, the method introduced in this article certainly can’t be comparable with 360, long ago there is a net to clean up garbage. Bat file can also clean up the computer garbage files. The main purpose of this article is to learn Python’s OS modules in a different way.
One, clean up the target
File types in system disk %system% :
[temporary Files (*.tmp)] [Temporary Files (*._mp)] [Log Files (*.log)] [Temporary Help Files (*.gid)] [Disk Check Files (*.chk)] [Temporary Backup Files (*.old)] [Excel Backup Files (*.xlk)] [temporary backup file (*.bak)]Copy the code
Folder under user directory % userProfile %
[COOKIE] cookies\*.* [file usage record] recent\*.* [IE Temporary Files] Temporary Internet Files\*.* [Temporary Files folder] Temp\*.*. *.Copy the code
Windows directory %windir% folder
[prefetch data folder] prefetch\*.* [temporary files] temp\*Copy the code
Python os
The Python OS module provides a very rich way to deal with files and directories. It ADAPTS itself to different operating system platforms and performs corresponding operations according to different platforms. In Python programming, you often have to deal with files and directories. Specific but more introduction, you can take a look at the official documents, there is no need to study, which know how to check the line.
https://docs.python.org/zh-cn/3/library/os.html
Copy the code
Python scripts
It’s better to build a wheel than to stand on the shoulders of giants. It’s already been written by a blogger in Python2, just change the print.
Import OS del_extension = {'. TMP ':' temporary files', '_mp' : 'temporary files _mp', 'log' : 'log file', 'gid' : 'temporary help file', 'CHK' : 'disk check file', 'the old' : 'temporary backup files',' XLK ':' Excel backup files', 'bak' : } del_userprofile = ['cookies', 'recent', 'Temporary Internet Files', 'Temp'] del_windir = ['prefetch', 'temp'] SYS_DRIVE = os.environ['systemdrive'] + '\\' USER_PROFILE = os.environ['userprofile'] WIN_DIR = os.environ['windir'] def del_dir_or_file(root): try: if os.path.isfile(root): os.remove(root) print ("file",root,"removed") elif os.path.isdir(root): os.rmdir(root) print("dir",root,"removed") except WindowsError: print("failure",root,"can't remove") def formatSize(b): Try: KB = b // 1024 except: print(" Error") return "Error" if KB > 1024: M = KB // 1024 if M > 1024: G = M // 1024 return "%dG" % G else: return "%dM" % M else: return "%dkb" % kb class DiskClean(object): def __init__(self): self.del_info = {} self.del_file_paths = [] self.total_size = 0 for i,j in del_extension.items(): self.del_info[i] = dict(name = j,count = 0 ) def scanf(self): for roots,dirs,files in os.walk(USER_PROFILE): for files_item in files: file_extension = os.path.splitext(files_item)[1] if file_extension in self.del_info: file_full_path = os.path.join(roots,files_item) self.del_file_paths.append(file_full_path) self.del_info[file_extension]['count'] += 1 self.total_size += os.path.getsize(file_full_path) def show(self): re = formatSize(self.total_size) for i in self.del_info: Print (self.del_info[I]["count"], self.del_info[I]["count"], self.del_info[I]["count"]) return re def delete_files(self): for i in self.del_file_paths: print(i) del_dir_or_file(i) if __name__ == "__main__": Print (" initialize garbage cleaner ") cleaner = DiskClean() print(" start to scan garbage file please wait for \n") cleaner.scanf() print(" scan successfully, ") re = cleaner.show() cleaner.delete_files()Copy the code
Save it as kill360.py in python’s working directory, which can be viewed by running os.getcwd(). You also need to make sure that the python installation directory has been added to the PATH system variable. On your computer, right-click my computer, properties, Advanced System Settings, environment variables
CMD open the command line and enter the Python command, as shown in the following figure:
Then create a new kill360.bat file and type:
python clean.py
Copy the code
Put it in the working directory along with the.py file
Then open the task scheduler
Create tasks and configure them
And then, you know, rest assured.