Python无法从返回的字符串

时间:2017-05-14 03:20:52

标签: python string winapi

我试过.replace,我试过装饰我已经尝试了几乎所有的解决方案,让这些该死的白色空间让我一个人但是它们不会消失。

import win32api
import string
import re

disks = win32api.GetLogicalDriveStrings()
disks = disks.translate({ord(c): None for c in ':\\ '})

disks = re.sub(r'\s+','',disks, flags=re.UNICODE)

print (disks)

for disk in disks.strip().split(): # or just disks without the other stuff
    print('Trying drive: '+disk)
    try:
        drives,_,_,_,_ = win32api.GetVolumeInformation(disk+':\\')
        print ('Got info for drive: ' + disk)
        print(drives)
    except Exception as details:
        print('Ooops, not a hard drive')

输出没有.strip()。split()

== RESTART: C:\~~~~~~ ==
C D E F 
Trying drive: C
Got info for drive: C
Windows
Trying drive:  
Ooops, not a hard drive
Trying drive: D
Ooops, not a hard drive
Trying drive:  
Ooops, not a hard drive
Trying drive: E
Got info for drive: E
Media 2
Trying drive:  
Ooops, not a hard drive
Trying drive: F
Ooops, not a hard drive
Trying drive:  
Ooops, not a hard drive

输出没有.strip()。split()

== RESTART: C:\~~~~ ==
C D E F 
Trying drive: C D E F 
Ooops, not a hard drive

现在尝试了

" ".join(disks.split())

1 个答案:

答案 0 :(得分:1)

您可以在split本身中包含所需的分隔符。类似的东西:

import win32api

disks = win32api.GetLogicalDriveStrings()
disks = disks.split(":\\\x00")

for disk in disks:
    if len(disk):
        print('Trying drive: '+disk)
        try:
            drives = win32api.GetVolumeInformation(disk+':\\')
            print ('Got info for drive: ' + disk)
            print(drives)
        except Exception as details:
            print('Ooops, not a hard drive')

对我来说,这会产生类似的东西:

Trying drive: C
Got info for drive: C
('OS', -1470383266, 255, 65470719, 'NTFS')
Trying drive: D
Ooops, not a hard drive
Trying drive: G
Ooops, not a hard drive
Trying drive: H
Ooops, not a hard drive
Trying drive: W
Got info for drive: W
('Offline', 0, 255, 6, 'CSC-CACHE')