首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python脚本在使用VS代码打开时找不到文件,但在终端上工作正常

Python脚本在使用VS代码打开时找不到文件,但在终端上工作正常
EN

Stack Overflow用户
提问于 2019-08-27 22:25:29
回答 2查看 4K关注 0票数 3

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

代码语言:javascript
复制
with open("input.txt", "r") as file:

并且在同一文件夹中有input.txt。问题是,当我用终端启动它时,就像

代码语言:javascript
复制
keddad@keddad-pc:~/bioinformatics-algorithms/1.3/PatternMatcher$ python3 ~/bioinformatics-algorithms/1.3/PatternMatcher/main.py

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

代码语言:javascript
复制
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 Code启动我的scipts,让他们在相同的目录中找到文件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-29 02:25:29

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

代码语言:javascript
复制
{
    "version": "0.2.0",
    "configurations": [
        {
            ....,
            "cwd" : "${workspaceFolder}/${relativeFileDirname}"
        }
    ]
}
票数 13
EN

Stack Overflow用户

发布于 2019-08-27 22:43:51

你在vscode中的终端在这个位置:“~/bioinformatics algorithms”

这是终端执行python代码并搜索文件的地方。您必须将终端位置更改为"~/bioinformatics-algorithms/1.3/PatternMatcher".

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57676850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档