简体   繁体   English

VSCode-在CWD中打开文件

[英]VSCode - open file in cwd

My VS code debug folder is: C:\\Users\\EML\\Python Code 我的VS代码调试文件夹是: C:\\Users\\EML\\Python Code

and the file I am working with is in: C:\\Users\\EML\\Python Code\\Boggle In the same folder I have the file en-dict.txt but I get this error message 并且我正在使用的文件位于: 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'

This is my launch.json settings in VS Code: 这是我在VS Code中的launch.json设置:


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

I have looked at many StackOverflow answers and none have helped me yet. 我看过许多StackOverflow答案,但没有一个对我有帮助。

if you set your launch as: 如果您将启动设置为:

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

it will invoke the python in the same directory where it is located. 它将在它所在的同一目录中调用python。

Try opening C:\\Users\\EML\\Python Code\\Boggle in VS Code instead of C:\\Users\\EML\\Python Code as that will implicitly make the working directory be what you're expecting. 尝试在VS Code中打开C:\\Users\\EML\\Python Code\\Boggle而不是C:\\Users\\EML\\Python Code因为这将使工作目录隐式成为您期望的目录。 Otherwise you have to be very careful about what file you have open when you launch the debugger to make sure that ${fileDirname} gets set appropriately. 否则,在启动调试器时,必须非常小心打开的文件,以确保正确设置了${fileDirname} Otherwise you can hard-code the working directory in your launch.json . 否则,您可以在launch.json对工作目录进行硬编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM