'IOError: [Errno 2] No such file or directory: for conda env
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
Solution 1:[1]
""" 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().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
