Python script for monitoring USB and copying files upon insertion

1k Views Asked by At

I would like to run a python script on my Raspberry Pi which basically monitors the USB ports for insertion of a USB drive. Upon insertion it should copy files from the drive onto a local folder.

So far I've managed to use the pyudev library to monitor insertion and removal of USB drives and obtain the device path but I'm not sure on how to proceed with copying the files.

My code so far is below:

from pyudev import Context, Monitor, MonitorObserver

context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
def print_device_event(device):
    print('background event {0.action}: {0.device_path}'.format(device))
observer = MonitorObserver(monitor, callback=print_device_event, name='monitor-observer')
observer.daemon
observer.start()
1

There are 1 best solutions below

0
FormulaRossa On

To copy files use:

import shutil
shutil.copyfile(src, dst)