2

I can easily read a csv file if I go:

pd.read_csv(r'C:\\yourpath\yourcsvfile.csv')

But I wanna try path variables instead of hardcode my path. So I tried:

processed_data_path=os.path.join(os.path.pardir,'data','processed')
train_file_path=os.path.abspath(os.path.join(processed_data_path,'train.csv'))
test_file_path=os.path.abspath(os.path.join(processed_data_path,'test.csv'))

the print result is:

C:\yourpath\train.csv
C:\yourpath\test.csv

So it looks alright. But when I tried the code below it throws a file does not exist error:

train_df = pd.read_csv(train_file_path)

where is the problem?

Gene Xu
  • 609
  • 1
  • 8
  • 18

3 Answers3

2

I would use pathlib module, which is very convenient and natively supported by modern Pandas versions:

Demo:

try:
    from pathlib import Path
except ImportError:             # Python 2
    from pathlib2 import Path

In [49]: p = Path('D:\\')

use overloaded operator / for joining paths:

In [50]: f = p / 'temp' / '.data' / '1.txt'

In [51]: f
Out[51]: WindowsPath('D:/temp/.data/1.txt')

In [52]: pd.read_csv(f)
Out[52]:
        Val
0  0.120000
1  0.320000
2  0.439999
3  0.560000
4  0.599999
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
1

Maybe adding a check before calling the parser will help you to identify the issue and it is always a good pratice to check path before parsing to make sure we dont't hit the wall of exceptions :

if os.path.exists(train_file_path):
           train_df = pd.read_csv(train_file_path)
else:
           print(train_file_path)
toheedNiaz
  • 1,435
  • 1
  • 10
  • 14
0

For VSCode users: The issue might be that VSCode sets current working directory to workspace root folder. My solution was:

To set current working directory to whatever file you are executing at the time:

File > Preferences > Settings > Python > Data Science > Execute in File Dir

Thanks to user brch: Python in VSCode: Set working directory to python file's path everytime

Jake
  • 101
  • 1
  • 5