【发布时间】:2012-09-13 02:30:08
【问题描述】:
假设我要制作一个文件:
filename = "/foo/bar/baz.txt"
with open(filename, "w") as f:
f.write("FOOBAR")
这给出了IOError,因为/foo/bar 不存在。
自动生成这些目录的最 Pythonic 方式是什么?我是否有必要在每一个上显式调用 os.path.exists 和 os.mkdir(即 /foo,然后 /foo/bar)?
【问题讨论】: