How to Build a Raspberry Pi USB Data Grabber with Python
This tutorial shows how to configure a Raspberry Pi to automatically detect inserted USB drives, use a Python script to record file names and paths into a text file, and optionally send the data via WeChat, while also explaining code structure and possible extensions.
Materials
Raspberry Pi, two smartphones logged into WeChat (as friends), and a USB flash drive.
Automatic USB detection on Raspberry Pi
Because the Pi runs Linux, you need to add a udev rule to auto‑mount inserted USB drives. sudo nano /etc/udev/rules.d/10-usbstorage.rules Paste the provided script (see image) and save. After inserting the USB, it will be mounted under /media/Untitled-sda1, which the script will use.
Python script
The script monitors the mount point, waits for a USB, then records file names and paths into a txt file ( u-copy.txt) stored in /home/pi/usbdisk-copy/ and sends it via WeChat.
It also checks for duplicate processing to avoid repeatedly generating the file.
Code explanation
The code first checks whether the directory /media/Untitled-sda1 exists; if not, it sleeps 10 seconds and retries. When the drive appears, function usbWalker() creates the txt file, and getusb() prevents duplicate runs.
Possible extensions
The same logic can be adapted for Windows, adding file‑copy functions, filtering by file type, or removing the WeChat step and storing copies locally. The Windows version can be packaged with PyInstaller as an .exe and run as a background service.
Use this tool responsibly; unauthorized data collection is illegal.
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.
