Python 2.6文件存在errno 17, [英] Python 2.6 File exists errno 17,

查看:289
本文介绍了Python 2.6文件存在errno 17,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个文件已经存在,我该如何摆脱错误17并发出警告信息?

If a file already exists how do I get rid of the error 17 and make a warning message instead?

import os, sys

# Path to be created
path = "/tmp/home/monthly/daily"

try:
   os.makedirs(path)
except OSError as e:
  if e.errno == 17:
     //do something

os.makedirs( path, 0755 );

print "Path is created"

但是它仍然显示ERRNO 17信息。我可以做什么?

However it still shows the ERRNO 17 message. What can I do?

推荐答案

第一次调用 os.makedirs 将创建该目录。 (或者如果目录已经存在,则不会更改)

After the first call to the os.makedirs the directory will be created. (or no change if the directory was already there)

第二个调用将始终引发异常。

The second call will always raise the exception.

删除第二次调用 makedirs

try:
    os.makedirs(path, 0755)
except OSError as e:
    if e.errno == 17:  # errno.EEXIST
        os.chmod(path, 0755)

# os.makedirs( path, 0755 )  <----

这篇关于Python 2.6文件存在errno 17,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆