Python - 跨平台文件路径

时间:2021-04-02 21:35:19

标签: python filepath

我的脚本通常使用 mac,但现在我在 Windows 上遇到了问题。

有没有什么好方法可以让路径为跨平台做好准备?

def load_json(store):
    storename = store
    
    with open('./stores/' + store +'.json' ,'r') as store:
        data = store.read()
    
    object = json.loads(data)

在 Windows 上我收到错误

[Errno 2] No such file or directory: './stores/alternate.json'

1 个答案:

答案 0 :(得分:0)

找到方法:

在 Windows 上从 VS Code 编辑 launch.json 文件并添加:

"cwd": "${fileDirname}"

现在 VS Code 在正确的目录中启动,而不是在 D:\GitHub