Python os.getcwd() is not working on subfolders in VSCODE

Viewed 4600

I have a python file, converted from a Jupiter Notebook, and there is a subfolder called 'datasets' inside this file folder. When I'm trying to open a file that is inside that 'datasets' folder, with this code:

import pandas as pd
# Load the CSV data into DataFrames
super_bowls = pd.read_csv('/datasets/super_bowls.csv')

It says that there is no such file or folder. Then I add this line

os.getcwd()

And the output is the top-level folder of the project, and not the subfolder when is this python file. And I think maybe that's the reason why it's not working.

So, how can I open that csv file with relative paths? I don't want to use absolute path because this code is going to be used in another computers.

Why os.getcwd() is not getting the actual folder path?

6 Answers

My observation, the dot (.) notation to move to the parent directory sometimes does not work depending on the operating system. What I generally do to make it os agnostic is this:

import pandas as pd
import os

__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

super_bowls = pd.read_csv(__location__ + '/datasets/super_bowls.csv')

This works on my windows and ubantu machine equally well. I am not sure if there are other and better ways to achieve this. Would like to hear back if there are.

(edited)

Per your comment below, the current working directory is

/Users/ivanparra/AprendizajePython/

while the file is in

/Users/ivanparra/AprendizajePython/Jupyter/datasets/super_bowls.csv

For that reason, going to the datasets subfolder of the current working directory (CWD) takes you to /Users/ivanparra/AprendizajePython/datasets which either doesn't exist or doesn't contain the file you're looking for.

You can do one of two things:

(1) Use an absolute path to the file, as in

super_bowls = pd.read_csv("/Users/ivanparra/AprendizajePython/Jupyter/datasets/super_bowls.csv")

(2) use the right relative path, as in

super_bowls = pd.read_csv("./Jupyter/datasets/super_bowls.csv")

There's also (3) - use os.path.join to contact the CWD to the relative path - it's basically the same as (2). (you can also use

The answer really lies in the response by user2357112: os.getcwd() is working fine. The problem is in your expectations. The current working directory is the directory where Python is running, not the directory of any particular source file. – user2357112 supports Monica May 22 at 6:03

The solution is:

    data_dir = os.path.dirname(__file__)

Try this code

super_bowls = pd.read_csv( os.getcwd() + '/datasets/super_bowls.csv')

Thanks to everyone that tried to help me. Thanks to the Roy2012 response, I got a code that works for me.

import pandas as pd
import os

currentPath = os.path.dirname(__file__)

# Load the CSV data into DataFrames
super_bowls = pd.read_csv(currentPath + '/datasets/super_bowls.csv')

The os.path.dirname gives me the path of the current file, and let me work with relative paths.

'/Users/ivanparra/AprendizajePython/Jupyter'

and with that it works like a charm!!

P.S.: As a side note, the behavior of os.getcwd() is quite different in a Jupyter Notebook than a python file. Inside the notebook, that function gives the current file path, but in a python file, gives the top folder path.

I noticed this problem a few years ago. I think it's a matter of design style. The problem is that: your workspace folder is just a folder, not a project folder. Most of the time, your relative reference is based on the current file.

VSCode actually supports the dynamic setting of cwd, but that's not the default. If your work folder is not a rigorous and professional project, I recommend you adding the following settings to launch.json. This is the simplest answer you need.

"cwd": "${fileDirname}"
Related