Python 脚本在用 VS 代码打开时找不到文件,但在终端上工作正常 [英] Python script can't find file when opening with VS code, but works OK with terminal

查看:19
本文介绍了Python 脚本在用 VS 代码打开时找不到文件,但在终端上工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 脚本,它可以使用普通的文件处理一些文件

I've got a python script which works with some files using normal

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

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

And there is input.txt in the same folder. Problem is, when I launch it with terminal, like

keddad@keddad-pc:~/bioinformatics-algorithms/1.3/PatternMatcher$ python3 ~/bioinformatics-algorithms/1.3/PatternMatcher/main.py

它运行良好,但是当我尝试使用 VS Code 调试器/不使用调试器运行它时,它只是找不到文件:

It works well, but when I try to run it with VS Code Debugger/Without debugger, it just can't find the files:

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 启动我的 scipt,让它们在相同的目录中找到文件?

How can I make VS Code launch my scipts in a way that they find the files in the same directories?

推荐答案

启动配置将CWD更改为文件夹

In the launch config change the CWD to the folder

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

这篇关于Python 脚本在用 VS 代码打开时找不到文件,但在终端上工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆