Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

How to Build a Raspberry Pi USB Data Grabber with Python

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
PythonautomationUSBRaspberry Pi

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion