【问题标题】:How do I create a file inside a directory that I created? [duplicate]如何在我创建的目录中创建文件? [复制]
【发布时间】:2019-07-17 11:42:42
【问题描述】:

我想创建一个目录,然后在该目录中创建一些文件。

我已经创建了一个这样的目录:

if not os.path.exists("output"):
    os.mkdir("output")

我现在如何访问这个目录并使用类似 open("foo.txt", "w") 的东西创建一些文件?

【问题讨论】:

标签: python


【解决方案1】:

将目录名称添加到您的文件名:

with open('output/foo.txt', 'w') as f:
    f.write('some content')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    相关资源
    最近更新 更多