SoFunction
Updated on 2024-10-29

Clear temporary files from removable hard disk with python

1. Target Scene

Those of you who have used Mac OS should have encountered that daily file operations will generate some specific temporary files in the same level directory.

Usually when you pack a folder into a zip file or copy a folder to a removable hard disk, temporary files will be included in it, and if it is the source code of a program, compiling it under Windows system will sometimes be affected.

The purpose of this post is to delete these temporary files using Python automation and Windows services while the removable drive is plugged into the computer.

2. Writing code

First, we need to cycle through the PC's drives to determine if a removable disk exists.

# Loop traversal, judgment for each drive #

while True:

        for item in disk_partitions():

            if 'removable' in :

                driver, opts = , 

                # Delete temporary files

                pass

                break

            else:

                continue

        # Hibernate for 5s and continue traversing

        sleep(5)

If the presence of a removable disk is detected, iterates through the files and deletes the Mac temporary files.

def remove_all_file(filepath, file_name):

    """

    Delete MAC temporary files

    :param file_name.

    :return.

    """

    for root, dirs, files in (filepath):

        for name in files:

            print(name)

            if ("._") or name == file_name:

                ((root, name))

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

# Install pyinstaller

pip3 install pyinstaller 

# Package py files into exe executables

# icon:

pyinstaller -F -i   del_mac_files.py

To ensure that the program is always running in the background, we need to create a system service that will execute the executable in the background all the time.

and these two files can be very convenient to create system services. One of them is used to create and delete services; it is used to ensure that the services can start normally.

# Registration services

# Service name

:\ del_mac_tempfile c:\

Note that when running the Register Service command, you need to run CMD with administrator privileges to execute it.

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

# Directory of registry services

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\

Find the service you created above in the registry, create a new entry for Parameters, and within the entry create two more strings that point to the executable and the full directory of the executable.

Finally, the command opens Service Management, finds the target service, sets Allow Service and Desktop Interaction, sets Enable Service and sets it to Self-Start.

3. Results Conclusion

With the above registered service, every time you plug in your removable hard disk, it will automatically go through the files on the removable hard disk drive, find the Mac temporary files and delete them.

Above is the use of python to clear temporary files in the removable hard disk in detail, more information about python clear files please pay attention to my other related articles!