我看到在解析共享路徑一些怪異的行爲(在服務器上共享的路徑,如\存儲\編譯)os.listdir等無法共享的Windows路徑上(Python的2.5)
我讀它包含目錄的文本文件我想進一步處理的路徑。爲了做到這一點,我做如下:
def toWin(path):
return path.replace("\\", "\\\\")
for line in open(fileName):
l = toWin(line).strip()
if os.path.isdir(l):
print l # os.listdir(l) etc..
這適用於本地目錄,但在共享系統上指定的路徑失敗。
e.g.
E:\Test -- works
\\StorageMachine\Test -- fails [internally converts to \\\\StorageMachine\\Test]
\\StorageMachine\Test\ -- fails [internally converts to \\\\StorageMachine\\Test\\]
但是,如果我打開python shell,導入腳本並調用具有相同路徑字符串的函數,那麼它的工作原理!
看來解析Windows共享路徑在兩種情況下的行爲都不同。
任何意見/建議?
你的解釋很可能是錯誤的。 os。*函數完全能夠處理包含反斜槓的路徑,只要您的操作系統能夠解釋它們即可。看到我自己的回答你的問題。 – 2010-01-25 16:45:08