首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当文件夹存在时,os.path.isdir返回false?

当文件夹存在时,os.path.isdir返回false?
EN

Stack Overflow用户
提问于 2014-07-26 05:26:48
回答 1查看 14.9K关注 0票数 6

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

代码语言:javascript
复制
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“。下面是输出:

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-26 20:08:17

download中的dir末尾有空格,而交互会话中定义的dir没有空格。这种差异是通过打印repr(dir)发现的。

代码语言:javascript
复制
In [3]: os.path.isdir('/tmp')
Out[3]: True

In [4]: os.path.isdir('/tmp\n')
Out[4]: False
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24964751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档