如何检查目录是否存在,如有必要,请创建? [英] How to check if a directory exists and create it if necessary?

查看:150
本文介绍了如何检查目录是否存在,如有必要,请创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最优雅的方法是检查文件目录是否存在,如果不存在,则使用Python创建目录?这是我试过的:

What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:

import os

file_path = "/my/directory/filename.txt"
directory = os.path.dirname(file_path)

try:
    os.stat(directory)
except:
    os.mkdir(directory)       

f = file(filename)

不知何故,我错过了code> os.path.exists (感谢kanja,Blair和Douglas)。这是我现在所在:

Somehow, I missed os.path.exists (thanks kanja, Blair, and Douglas). This is what I have now:

def ensure_dir(file_path):
    directory = os.path.dirname(file_path)
    if not os.path.exists(directory):
        os.makedirs(directory)

是否有open的标志,这样会自动发生?

Is there a flag for "open", that makes this happen automatically?

推荐答案

我看到两个答案好的品质,每个都有一个小小的缺陷,所以我会给我做的:

I see two answers with good qualities, each with a small flaw, so I will give my take on it:

尝试 os.path.exists ,并考虑 os.makedirs 创建。

Try os.path.exists, and consider os.makedirs for the creation.

if not os.path.exists(directory):
    os.makedirs(directory)

如注释和其他地方所述,有一个竞争条件 - 如果目录是在$ code> os.path.exists 和 os.makedirs 调用, os.makedirs 将失败,一个 OSError 。不幸的是,覆盖 OSError 并继续并不是万无一失的,因为它会忽略由于其他因素(如权限不足,完整磁盘等)而导致的目录创建失败。

As noted in comments and elsewhere, there's a race condition - if the directory is created between the os.path.exists and the os.makedirs calls, the os.makedirs will fail with an OSError. Unfortunately, blanket-catching OSError and continuing is not foolproof, as it will ignore a failure to create the directory due to other factors, such as insufficient permissions, full disk, etc.

一个选项是捕获 OSError 并检查嵌入式错误代码,如果知道什么(on我的操作系统13似乎表明权限被拒绝,17该文件存在 - 这不是很清楚,甚至远程可移植,但是在有没有跨平台的方式从Python的OSError获取信息)。或者,可能有第二个 os.path.exists ,但假设另一个在第一次检查后创建了目录,然后在第二个检查之前将其删除 - 我们仍然可以被愚弄。

One option would be to trap the OSError and examine the embedded error code, if one knew what's what (on my OS, 13 seems to indicate that permission is denied, and 17 that the file exists - it's not clear that that's even remotely portable, but is explored in Is there a cross-platform way of getting information from Python’s OSError). Alternatively, there could be a second os.path.exists, but suppose another created the directory after the first check, then removed it before the second one - we could still be fooled.

根据应用程序的不同,并发操作的危险可能大于或等于其他因素(如文件权限)所造成的危险。开发人员在选择实施之前,必须更多地了解正在开发的特定应用程序及其预期的环境。

Depending on the application, the danger of concurrent operations may be more or less than the danger posed by other factors such as file permissions. The developer would have to know more about the particular application being developed and its expected environment before choosing an implementation.

这篇关于如何检查目录是否存在,如有必要,请创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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