即使目录存在,Python的isdir函数也会返回false(Windows 10)

时间:2019-09-03 08:19:58

标签: python python-3.x windows path

我想检查目录“ C:\ Username \ Documents”是否存在目录(称为“ P1”)-> C:\ Username \ Documents \ P1,但始终返回false。

目录名称(在此示例中为P1)位于单独的.txt文件中,在检查目录是否存在之前会读取该文件。

我已经使用os.path.join将我的Documents-path与“ P1”文件夹连接起来并打印了结果(看起来不错),但是检查时始终返回false。

我尝试了各种变体来将路径分配给变量(联接,将r置于完整Documents-path的前面,等等,将路径与join和os.separate组合在一起)。

import os
import shutil
import sys

packet_obj = open(sys.argv[1], "r") #P1 is in this list
FullPath = os.path.join('C:',os.sep,'Users','MYUSERNAME','Documents') #tried different variants, e.g. assigning the full path with an r in front of etc.

for line in packet_obj:

    if (os.path.isdir(os.path.join(newFullPath, line))): #should be C:\MYUSERNAME\Documents\P1

        print("Works")
    else:
        print("Doesn't work!")    

if查询总是返回false,所以我收到“不工作”消息。

0 个答案:

没有答案