2016-09-14 63 views
2

我試圖使用os.path.isfile來測試網絡驅動器上是否存在文件,但即使文件存在,它也會返回false。任何想法,爲什麼這可能是我可以用來檢查這個文件的任何其他方法?os.path.isfile()對網絡驅動器上的文件返回false

我使用Python2.7和Windows10

這個,因爲它應該返回true:

import os 

if os.path.isfile("C:\test.txt"): 
    print "Is File" 
else: 
    print "Is Not File" 

這個返回false,即使該文件存在:

import os 

if os.path.isfile("Q:\test.txt"): 
    print "Is File" 
else: 
    print "Is Not File" 
+0

使用原始字符串:'os.path.isfile(r「Q:\ test.txt」)'? –

+0

也返回false –

+0

'os.path.exists()'返回什麼? –

回答

2

蟒蛇https://docs.python.org/2/library/os.path.html

的os.path中模塊總是適合於操作系統的Python在其上運行,並且因此可用於本地路徑

使用完整的UNC路徑,而不是映射的驅動器嘗試的路徑的模塊。

import os 

if os.path.isfile(r"\\full\uncpath\test.txt"): 
    print "Is File" 
else: 
    print "Is Not File" 
+0

謝謝你這個作品! –

+0

@TomCarroll甜蜜!我很高興能幫助你 –

相關問題