Wenn man an os.path.abspath() als Pfad lediglich einen Windows-Laufwerksbuchstaben (mit Doppelpunkt) übergibt, sollte das Ergebnis einen abschließenden Backslash enthalten. Beispiel:
Code: Alles auswählen
import os
print os.path.abspath("C:")
# results: "C:"
Code: Alles auswählen
import os
print 'CWD is %s' % os.getcwd()
path = "C:"
print 'os.path.abspath("%s") results %s' % (path, os.path.abspath(path))
path = "D:"
print 'os.path.abspath("%s") results %s' % (path, os.path.abspath(path))
Nun begebt euch mal mit der Konsole jeweils nach C:\Temp bzw. D:\Temp und führt das Skript von dort aus aus. Hier was bei mir passiert:
Seht ihr was ich meine? Python "merkt" sich irgendwie das 'Current Working Directory' von jedem Laufwerk und gibt bei print os.path.abspath("C:") plötzlich das zuletzt benutzte 'CWD' anstelle 'C:\' aus.cmd.exe hat geschrieben:C:\Dokumente und Einstellungen\user>cd c:\temp
C:\Temp>%ProgramFiles%\Python24\python.exe bug_os.path.abspath.py
CWD is C:\Temp
os.path.abspath("C:") results C:\Temp
os.path.abspath("D:") results D:\
C:\Temp>d:
D:\>cd d:\temp
D:\temp>%ProgramFiles%\Python24\python.exe bug_os.path.abspath.py
CWD is D:\temp
os.path.abspath("C:") results C:\Temp
os.path.abspath("D:") results D:\temp
D:\temp>c:
C:\Temp>cd c:\windows\system32\drivers
C:\WINDOWS\system32\drivers>d:
D:\temp>%ProgramFiles%\Python24\python.exe bug_os.path.abspath.py
CWD is D:\temp
os.path.abspath("C:") results C:\WINDOWS\system32\drivers
os.path.abspath("D:") results D:\temp
D:\temp>cd wol
D:\temp\wol>%ProgramFiles%\Python24\python.exe d:\temp\bug_os.path.abspath.py
CWD is D:\temp\wol
os.path.abspath("C:") results C:\WINDOWS\system32\drivers
os.path.abspath("D:") results D:\temp\wol
D:\temp\wol>
Ist das denn so gewollt?