您的位置:首页 > 编程语言 > Python开发

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

2015-04-23 20:48 435 查看
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")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: