我有以下代码来检查目录是否存在
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"),时,它返回"true“。
当文件夹存在时,为什么会显示为false?
发布于 2014-07-26 20:08:17
download中的dir末尾有空格,而交互会话中定义的dir没有空格。这种差异是通过打印repr(dir)发现的。
In [3]: os.path.isdir('/tmp')
Out[3]: True
In [4]: os.path.isdir('/tmp\n')
Out[4]: Falsehttps://stackoverflow.com/questions/24964751
复制相似问题