Search code examples
bashpython-3.xraspberry-piraspbian

Ejecting/unmounting random USB flash drive in Raspberry pi / Python


So I'm trying to get a working code in Python that will eject/unmount all USB flash drives attached to the Pi (Running Raspbian) - so that they can be removed safely. The final code will be run from within the python program.

Additionally, I'd like to eject/unmount the USB flash drive even if it's in use.

I've looked around and can't see how to do this. Thanks.


udisks --detach /media/pi/DOCS/ - 'Blocked device... Resource temporarily available'...

udisks --detach /media/pi/ - 'Blocked device...Resource temporarily available'...

udisks --detach /media/ - 'Blocked device...Resource temporarily available'...

sudo udisks --detach /media/pi/DOCS/ - still blocked...

sudo umount /path/to/devicename - command not found...

eject /media/pi/DOCS/ - Unable to open '/dev/sda'

(DOCS is the name if my USB flash drive. - though I want to eject all USB flash drives - not just my one)


So I'm going to ask the user in Python to select their USB flash drive from a list, which is pretty easy (just read in the folder) - so I will have the pathway to the USB. I'm still not sure which code can safely disconnect the USB flash drive - Maybe more research is the answer. Thanks for your help so far.


Solution

  • For udisks --detach the parameter should be the device, not the mounting point. For example, if the USB Disk is /dev/sdb the command would be udisks --detach /dev/sdb

    If the command still doesn't work you could try udiskctl power-off -b <device> which should also work.