Python: get name of a USB flash drive device [windows]

前端 未结 3 1501
走了就别回头了
走了就别回头了 2021-01-20 09:14

I\'m trying to write little program which will be able to read some informations about REMOVEABLE_DEVICE (USB). I\'ve tried pyusb but I was not able to pull data I need.

3条回答
  •  灰色年华
    2021-01-20 09:48

    I will look odd but it will work (get list of all removable volume's Label's)

    import os
    os.system('echo list volume > Ravi.txt')
    path1 = os.path.join(os.getcwd(),"Ravi.txt")
    os.system('diskpart /s '+path1+' > logfile.txt')
    path2 = os.path.join(os.getcwd(),"logfile.txt")
    
    Str = open(path2).read()
    Str = Str.split('\n')
    matching = [s for s in Str if "Removable" in s]
    
    for i in matching:
        i = ' '.join(i.split())
        i = i.split(" ")
        print i[3]+"("+i[2]+":)"
    

    output

    MYPENDRIVE(D:)
    

提交回复
热议问题