Visual Studio python读取文件的相对路径

问题描述 投票:0回答:1

我在visual studio中打开了一个python项目文件夹并运行getter.py,其中包含以下代码:

getter.py
with open('instruments.txt', 'r') as f:
    instrument_list = f.read().splitlines()

我的文件夹树如下:

proj
    -downloaders
        --getter.py
    instruments.txt
    signals.py

如您所见,getter.py位于donwloaders文件夹中。为什么visual studio在没有FileNotFoundError的情况下运行上面的脚本?请注意,当我将instructions.txt移动到downloaders文件夹时,它会抛出FileNotFoundError。我应该更改设置中的内容吗?

python visual-studio path
1个回答
1
投票

确保将txt文件放在python脚本目录中,而不是python项目目录中。

proj
    -downloaders
        --getter.py
        --instruments.txt
    signals.py
© www.soinside.com 2019 - 2024. All rights reserved.