Is there a way go get a list of connected storage devices, like Cameras, SD cards and external Hard Drives, in Python?
Asked
Active
Viewed 8,120 times
8
-
1How do you define 'storage device'? How 'connected'? – Oct 01 '12 at 12:11
-
@Tichodroma The list of external devices that appears in the Mac Finder, Windows Explorer or Ubuntu file browser. – Adam Matan Oct 01 '12 at 12:34
-
1I wouldn't mind finding internal drives too - so the definition is not so important. – Adam Matan Oct 01 '12 at 18:44
1 Answers
5
The following should work for Linux and Windows. This will list ALL drives, not just externals!
import subprocess
import sys
#on windows
#Get the fixed drives
#wmic logicaldisk get name,description
if 'win' in sys.platform:
drivelist = subprocess.Popen('wmic logicaldisk get name,description', shell=True, stdout=subprocess.PIPE)
drivelisto, err = drivelist.communicate()
driveLines = drivelisto.split('\n')
elif 'linux' in sys.platform:
listdrives=subprocess.Popen('mount', shell=True, stdout=subprocess.PIPE)
listdrivesout, err=listdrives.communicate()
for idx,drive in enumerate(filter(None,listdrivesout)):
listdrivesout[idx]=drive.split()[2]
# guess how it should be on mac os, similar to linux , the mount command should
# work, but I can't verify it...
elif 'macosx' ...
do the rest....
The above method for Linux is very crude, and will return drives like sys
and procfs
etc., if you want something more fine tuned, look into querying with python-dbus
.
-
1Probably if you add a step that detects which of the mount points are owned by current user (presumably the desktop logged-in user), the list will be very close to the list of 'external devices'. You should be able to unmount first what you want to unplug. – 9000 Oct 01 '12 at 17:15
-
1here's [an example how to use Udisks via dbus](http://stackoverflow.com/a/5081937/4279). Property [DeviceIsRemovable](http://hal.freedesktop.org/docs/udisks/Device.html#Device:DeviceIsRemovable) might be close. – jfs Oct 01 '12 at 18:40
-
1