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.
Reading font
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.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.