Search code examples
pythondataframevisual-studio-codepathpickle

Relative paths with multiple file types and collaborators


When I want to access a pickle data file in a sibling folder, I cannot use the same (relative) paths. Because I work with multiple collaborators, this results in having to change the file_path variable (see snippets below) after each git push/pull, which is annoying, and probably unnecessary.

File structure looks like this:

workspace_folder
    -data
        -.raw
            -.-data.pickle
    -src
        -.data_analyis.py
        -.rapid_experimentation.ipynb

My collaborator wrote the following code that works on his pc in data_analysis.py:

# '..' to access sibling folder
file_path = r"../data/raw/data.pickle"

# read file
with open(file_path, 'rb') as f:
    data = pickle.load(f)

This gives me a file not found error, since VSCode assumes that the root folder is workspace_folder, and not the location of data_analysis.py. So I have to use:

# start from work_space folder, so no '..'
file_path = "data/raw/2022-03-13 conservative submissions"

# read file
with open(file_path, 'rb') as f:
    data = pickle.load(f)

However, when I use a Jupyter notebook, the first code snippet does work for me. Ergo: a .py and .ipynb file in the same src folder in VSCode do not use the same root folder when loading files with relative paths.

I would like a single solution that works for both my collaborators and myself, so that we can both run the .py file without errors or having to change the path before and after every git push/pull. Can somebody please explain what I'm misunderstanding?


Solution

  • You and your collaborators have different cwd sets. It looks like your collaborators have reset the cwd, such as add this in the settings.json file:

    "python.terminal.executeInFileDir": true,
    

    So the cwd of your collaborators was src folder() while you were workspace_folder.