Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to restore the immaculate mobile hard disk by Python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to restore a pure mobile hard disk by Python". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to restore a pure mobile hard disk by Python".

Write code

First, we need to loop through the PC's drive to see if there is a removable disk.

# Loop through each drive to determine while True: for item in disk_partitions (): if 'removable' in item.opts: driver, opts = item.device, item.opts # Delete temporary files pass break else: continue # hibernate 5s Continue traversing sleep (5)

If a removable disk is detected, traverse the file and delete the Mac temporary file.

Def remove_all_file (filepath, file_name): "" Delete temporary MAC file: param file_name:: return: "" for root, dirs, files in os.walk (filepath): for name in files: print (name) if name.startswith (". _") or name = = file_name: os.remove (os.path.join (root) Name))

Next, we use pyinstaller to package the Python file into an Exe executable file.

# install pyinstaller pip3 install pyinstaller # package the py file into an exe executable # icon: img.ico pyinstaller-F-I img.ico del_mac_files.py

To keep the program running in the background, we need to create a system service that keeps the executable running in the background.

Instsrv.exe and srvany.exe files can be very convenient to create system services. Instsrv.exe is used to create and delete services, and srvany.exe is used to ensure that services can be started normally.

# Registration Service # instsrv.exe Service name srvany.exe c:\ instsrv.exe del_mac_tempfile c:\ srvany.exe

It is important to note that when running the registration service command, you need to run CMD with administrator privileges to execute.

After registering the service, you also need to associate the service with the executable through the registry.

# registry service directory HKEY_LOCAL_MACHINE\ SYSTEM\ CurrentControlSet\ Services\

Find the service created above in the registry, create a new key for Parameters, and create two more strings inside the key, pointing to the executable and the full directory of the executable.

Finally, the services.msc command opens service management, finds the target service, sets the service to allow interaction with the desktop, sets the service to open, and sets it to self-starting.

Thank you for your reading, the above is "Python how to restore the pure mobile hard drive" content, after the study of this article, I believe that you have a deeper understanding of how Python restore the pure mobile hard disk, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report