6

我有以下代码检查目录是否存在

def download(id, name, bar):
    cwd = os.getcwd()
    dir = os.path.join(cwd,bar)
    partial = os.path.join(cwd, id + ".partial")
    print os.path.isdir(dir)
    if(os.path.isdir(dir)):
        print "dir exists"
        dir_match_file(dir, bar)
    else:
        print dir

对于实际存在的目录,它返回“False”。这是输出:

False
/scratch/rists/djiao/Pancancer/RNA-seq/CESC/TCGA-BI-A0VS-01A-11R-A10U-07

当我进入 python 交互会话并输入 os.path.isdir("/scratch/rists/djiao/Pancancer/RNA-seq/CESC/TCGA-BI-A0VS-01A-11R-A10U-07") 时,它返回“真的”。

为什么当文件夹存在时它会说假?

4

1 回答 1

11

dirin最后download有空格,而dir在交互式会话中定义的没有。差异是通过印刷发现的repr(dir)

In [3]: os.path.isdir('/tmp')
Out[3]: True

In [4]: os.path.isdir('/tmp\n')
Out[4]: False
于 2014-07-26T12:08:17.257 回答