首页 > 编程语言 > 详细

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

时间:2015-04-24 09:11:52      阅读:312      评论:0      收藏: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") 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!