IOError: [Errno 2] No such file or directory: for conda env

2.8k Views Asked by At

In my Python script main.py, I read a csv using pandas like this:

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

My setup looks like this:

project/
   src/ 
      input/
         input.csv
      output/
         output.csv
      main.py

When I try to run my Python script from within the srcfolder, it runs smoothly. However, when I do this from my projectfolder:

python src/main.py

it throws an error

IOError: [Errno 2] No such file or directory: './input/input.csv'

What am I missing out on? It's literally the same script

1

There are 1 best solutions below

0
SimoX On
""" main.py """

import pandas as pd
import os

crr_dir = os.getcwd()       # get the cwd
os.chdir(crr_dir + '/src')  # change the cwd
print(os.getcwd())          # check the new path

df = pd.read_csv('./input/input.csv')  # now you can run this!

With this code you can call main.py from the project folder (python src/main.py). If you have other needs, simply change your working dir with os.chdir().