【问题标题】:Visual Studio python reading files relative pathsVisual Studio python读取文件相对路径
【发布时间】:2018-07-10 12:23:16
【问题描述】:

我在 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】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      相关资源
      最近更新 更多