Python: OS Independent list of available storage devices

后端 未结 1 720
夕颜
夕颜 2021-02-04 15:50

Is there a way go get a list of connected storage devices, like Cameras, SD cards and external Hard Drives, in Python?

相关标签:
1条回答
  • 2021-02-04 16:23

    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.

    0 讨论(0)
提交回复
热议问题