python os.path.isfile返回false

时间:2015-06-05 06:02:37

标签: python

我正在尝试从不同的目录中搜索文件。我看到该文件确实存在,但os.path.isfile仍然返回false。

由于我的文件可以在多个目录中

import os

me = "/user"
my_home = "/home"
my_direcotry = "/user/jsbt"
my_file =  "/myfile"

my_path = me + my_home + my_directory + my_file

print(my_path)

if (os.path.isfile( my_path )) is True:
print ("Path was found")
else:
print ("Path does not exist")
sys.exit(0)

以上情况总是导致错误,无法找到该文件。

但我确实找到了我的文件

有任何暗示吗?

1 个答案:

答案 0 :(得分:0)

将扩展名添加到my_file并检查路径是否正确。

另外,不要使用是True。改为:

if (os.path.isfile( my_path )): # OR == True
    print ("Path was found")
    else:
    print ("Path does not exist")
    sys.exit(0)
相关问题