Python脚本在用VS代码打开时找不到文件,但可以使用termin

2024-05-13 02:57:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,它使用normal处理一些文件

with open("input.txt", "r") as file:

还有输入.txt在同一个文件夹中。问题是,当我用终端启动它时,比如

^{pr2}$

它工作得很好,但是当我尝试使用VS Code Debugger/不使用Debugger运行它时,它只是找不到文件:

keddad@keddad-pc:~/bioinformatics-algorithms$ cd /home/keddad/bioinformatics-algorithms ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /usr/bin/python3 /home/keddad/.vscode/extensions/ms-python.python-2019.8.30787/pythonFiles/ptvsd_launcher.py --default --nodebug --client --host localhost --port 46499 /home/keddad/bioinformatics-algorithms/1.3/PatternMatcher/main.py 
Traceback (most recent call last):
  "some traceback here"
  File "/home/keddad/bioinformatics-algorithms/1.3/PatternMatcher/main.py", line 19, in <module>
    main()
  File "/home/keddad/bioinformatics-algorithms/1.3/PatternMatcher/main.py", line 10, in main
    with open("input.txt", "r") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'input.txt'

如何让VS代码以它们在相同目录中找到文件的方式启动scips?在


Tags: 文件pytxthomeinputmainaswith
2条回答

launch config中,将CWD更改为文件夹

{
    "version": "0.2.0",
    "configurations": [
        {
            ....,
            "cwd" : "${workspaceFolder}/${relativeFileDirname}"
        }
    ]
}

你的vscode终端就在这个位置:“~/生物信息学算法”

这是被终端执行的python代码和搜索文件。您必须将终端位置更改为“~/bioinformatics algorithms/1.3/PatternMatcher”。在

相关问题 更多 >