2017-08-16 45 views
-1

如何使用python構建用於與腳本運行的操作系統不同的操作系統的路徑?在Python中爲不同的操作系統構建文件路徑

E.g.我在Linux上運行的腳本中,我要構建的Windows路徑

winPath = os.path.join(winRoot, dir, subdir) #doesn't work when script is run on linux 
+0

當然,沒有'winRoot'在Linux上。在Linux上,文件系統的根是'「/」'。 – DyZ

+0

@DYZ,對於錯字感到抱歉。是的,Linux上沒有windows根目錄;在Linux上,文件系統的根是「/」。我們絕對同意。然而,在Linux上,我想構建一個Windows文件路徑。在這個腳本中,winRoot被硬編碼爲「c:」 – cowlinator

回答

1

Check this doc page

導入ntpath模塊產生的窗口路徑。

winPath = os.join.path(winRoot, dir, subdir) 

成爲

winPath = ntpath.join(winRoot, dir, subdir) 
相關問題