python如何使用 os.path.exists()--Learning from * 分类: python 2015-04-23 20:48 139人阅读 评论(0) 收藏

时间:2023-03-10 02:18:45
python如何使用 os.path.exists()--Learning from *                                                    分类:            python             2015-04-23 20:48    139人阅读    评论(0)    收藏

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")

版权声明:本文为博主原创文章,未经博主允许不得转载。