2017-05-14 4 views
1

Ich habe versucht .replace, ich habe versucht Trim Ich habe versucht fast jede Lösung da draußen diese verdammten weißen Räume, um mich in Ruhe zu lassen, aber sie werden nicht weggehen.Python Cant Leerzeichen von zurückgegebenen String entfernen

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') 

Ausgang ohne .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 

Ausgang ohne .strip(). Split()

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

haben versucht nun

" ".join(disks.split()) 
+1

Ich glaube, Sie sollten die Dokumentation aufhören zu raten und lesen. In dieser Zeichenfolge sind keine Leerzeichen enthalten. –

Antwort

1

Sie können die erforderlichen Trennzeichen in dieeinfügenselbst. Etwas wie:

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') 

Für mich stellt dies so etwas wie:

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') 
+0

Ihr Code funktioniert aber [das Trennzeichen ist nur "\ 0"] (http://docs.activestate.com/activepython/3.4/pywin32/win32api__GetLogicalDriveStrings_meth.html). Edit: Code ist korrekt, weil Sie ": \\" für den Aufruf "GetVolumeInformation()" anhängen. – zett42

+0

Das funktioniert für mich, außer dass es am Ende einen Platz frei lässt, was jetzt ganz in Ordnung ist! Danke vielmals! – Xander

Verwandte Themen