【问题标题】:Save file in previous Directory w/out knowing full folder path | Python在不知道完整文件夹路径的情况下将文件保存在以前的目录中 | Python
【发布时间】:2021-07-13 09:21:43
【问题描述】:

例子:

filename = "foo"
contents = "bar"

with open('data/' + filename + '.json', 'w') as file:
    json.dump(contents, file)

但是,这将在当前工作目录中创建data/ 文件夹; src/.

我如何备份一个目录然后创建这个文件夹并保存文件;不使用完整的文件路径?

这个 Python 脚本可以在任何机器上运行;所以我不能使用完整路径。

【问题讨论】:

  • open('../data/' + filename + '.json', 'w') ?
  • 我现在试试这个

标签: python-3.x directory


【解决方案1】:

在您想要的文件路径前使用../

open('../data/' + filename + '.json', 'w')

谢谢,@MauriceMeyer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多