当前位置:首页 > Python教程 > python技巧

python如何使用 os.path.exists()--Learning from stackoverflow

Q&A参考连接
Problem:IOError: [Errno 2] No such file or directory。

os.path.exists() 如果目录不存在,会返回一个0值。
所以,如果你如下使用该函数,会得到 Problem 中描述的错误,而且错误会定位在其他地方:

            
                import os
try: 
    os.path.exists("E:/Contact") #Check if dir exist     except:
    os.mkdir("E:/Contact")   #if not, create
        

正确的使用方式如下:

            
                if
                not os.path.exists("E:/Contact"):
    os.mkdir("E:/Contact") 
        

原文:http://blog.csdn.net/paul_c_v/article/details/45226855


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐