【问题标题】:VSCode - open file in cwdVSCode - 在 cwd 中打开文件
【发布时间】:2019-06-20 04:51:39
【问题描述】:

我的 VS 代码调试文件夹是: C:\Users\EML\Python Code

我正在使用的文件位于: C:\Users\EML\Python Code\Boggle 在同一个文件夹中,我有文件 en-dict.txt 但我收到此错误消息

with open("en-dict.txt", "r", encoding="utf8") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'en-dict.txt'

这是我在 VS Code 中的 launch.json 设置:


    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "cwd": "${fileDirname}"
    }

我查看了许多 StackOverflow 答案,但没有一个对我有帮助。

【问题讨论】:

  • “调试文件夹”是什么意思?以及您在 VS Code 中打开了哪个文件夹(根据您的代码编写方式,它应该是 C:\Users\EML\Python Code\Boggle)。
  • 调试我的意思是我已经在C:\Users\EML\Python Code中打开了VS代码

标签: python visual-studio-code


【解决方案1】:

如果您将启动设置为:

{
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": ""
        }

它将调用它所在目录中的python。

【讨论】:

  • 谢谢。我刚刚尝试过,但仍然收到相同的错误消息
【解决方案2】:

尝试在 VS Code 中打开 C:\Users\EML\Python Code\Boggle 而不是 C:\Users\EML\Python Code,因为这会隐含地使工作目录成为您所期望的。否则,在启动调试器时必须非常小心打开的文件,以确保正确设置 ${fileDirname}。否则,您可以在 launch.json 中对工作目录进行硬编码。

【讨论】:

  • 谢谢。这是我设法实现的唯一解决方法。我希望有办法解决这个问题,而不必为每个新项目手动更改文件夹。耻辱
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 2016-07-24
相关资源
最近更新 更多