简体   繁体   中英

What's the correct way and VSCode setup to refer to files between subdirectories in Python

I have a folder with a few subfolders as here:

my-project/
  input/
    data.csv
  src/
    script.py

I want to read data from my-project/input/data.csv within script.py , so I have:

import pandas as pd
data = pd.read_csv('../input/data.csv')

However, my workspace is my-project so when I run script.py it returns:

Exception has occurred: FileNotFoundError [Errno 2] No such file or directory: '../input/data.csv

which is understandable as input is within my-project , not at the same level. However, referring with .. really feels like the correct way to refer to data.csv as we do it from script.py . But maybe I'm wrong?

In case this is a reasonable way to refer to my data file - how can I setup the VSCode to be able to run the script without returning the error? I think there should be a way to add the subfolder to searching path, without needing to open the subfolder as a workspace, but I had a bad luck trying to find it.

@Edit: Please note that I'm aware of the concept of relative/absolute paths. This questions is more about VSCode settings. I am also willing to accept the answer: "No, referring with ../input/data.csv is the dumb thing to do in this settings. You should refer with input/data.csv instead, because..." (it's contradictory with my current understanding, but I could be entirely wrong and I'd be happy to learn a different point of view)

I believe this is simpler than you thought, let do it together!

  1. 创建空文件夹

  2. 用 VSCode 打开它

The used extensions...

  1. 扩展

I believe the below steps are not so hard!

  1. 基本步骤

Switch the default interpreter to the created virtual environment

  1. 虚拟环境

Create a simple launch.json, with simple choice python script

  1. 创建启动.json

  2. Launch.json 文件示例

Guess what now! All we have to do now is select a script.py file in the editor then....... RUN!

  1. 运行脚本后

You can see the result in the terminal.

let's talk a bit...

The generated launch.json file will force us to select the **src.script.py" in the editor before we click the start button every time we want to launch the program, if you like so, I can suggest a more proper way

In step 6, you can choose Module instead of Python file , after that the editor will prompt you a field asking for the module name, our input must be src.script .

We will get our launch.json file like this...

新的launch.json

And now, we can start the program from where we want, which means if the opened file in the editor is "src/data.json" as an example, going to the debugger section and click start will always start the src/script.py file.

If you run the script in your workspace then input/data.csv should work. The location is not based on where your script is, but from where you run it.

To get the current working directory you can use

import os
print(os.getcwd())

and then go from there.

Edit: you can change the working directory in your launch.json as described here: https://stackoverflow.com/a/55072246/14246131

by setting launch.json

"cwd":"${fileDirname}"

// or use "cwd":"${workspaceFolder}/src" to specifically assign /src as your current working directory

then all the relative path starts from the py file you are running (in your case will be my-project/src ), you should be able to use:

data = pd.read_csv('../input/data.csv')

the launch.json variables can be referenced here: https://code.visualstudio.com/docs/editor/variables-reference


here's my sample env for your reference:

file structure:

my-project/
  .vscode/
    launch.json
  input/
    xxxxx.txt
  src/
    main.py

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd":"${fileDirname}"
        }
    ]
}

main.py:

with open('../input/xxxxx.txt', 'r') as file_input:
    res = file_input.read()
    print(res)

I would typically do the following -- figure out the directory where my current.py file is and use a relative path from there

import os
py_folder = os.path.dirname(os.path.abspath(__file__))
input_folder = os.path.join(py_folder, '../input')
data = pd.read_csv(os.path.join(input_folder', 'data.csv'))

this way it does not matter from which directory you run your.py, and it works for any development environment ie not VSCode specific

You are probably referring to the file like ../input/data.csv in relation to the path of your script.py

But that is not the correct way to go about it you should be adding the file path in relation to from where you are executing the script.py , which I assume is from the root of project folder most probably. Especially if you are using the run python command from VS code.

Hence why the path input/data.csv should work in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM