Arregator
Arregator

Reputation: 159

Pandas.read_csv() FileNotFoundError even though file exists

I try to run this piece of Python 3 code in my Anaconda Jupyter Notebook (same cell, nothing else in):

train  = pd.read_csv('tutorial\labeledTrainData.tsv', header=0, delimiter="\t", quoting=3) #OK!
test   = pd.read_csv('tutorial\testData.tsv',         header=0, delimiter="\t", quoting=3) #FileNotFoundError!

the first line runs just fine, but the second one gives error:

FileNotFoundError: [Errno 2] File b'tutorial\testData.tsv' does not exist: b'tutorial\testData.tsv'

dir command in the respective tutorial folder gives:

06.12.2019  15:38    <DIR>          .
06.12.2019  15:38    <DIR>          ..
05.05.2018  20:07        33 556 378 labeledTrainData.tsv
05.05.2018  20:07           282 796 sampleSubmission.csv
05.05.2018  20:07        32 724 746 testData.tsv
05.05.2018  20:07        67 281 491 unlabeledTrainData.tsv

i.e. the "problem" file testData.tsv IS there, next to the "good" labeledTrainData.tsv.

if I change the slash direction for the second line and run it like this:

train  = pd.read_csv('tutorial\labeledTrainData.tsv', header=0, delimiter="\t", quoting=3) #OK!
test   = pd.read_csv('tutorial/testData.tsv',         header=0, delimiter="\t", quoting=3) #OK!

then both lines run just fine. Same, they run fine if I put r before problem file name:

test   = pd.read_csv(r'tutorial\testData.tsv',         header=0, delimiter="\t", quoting=3) #OK!

I checked both files' attributes - none is hidden , nor read-only, permissions are equal, etc.

Changing lines order in the cell does not change the erorr - same file is problem...

I want to know what the problem pandas (or python?) sees in the second line of code, which I do not see?

Upvotes: 3

Views: 4288

Answers (3)

Ayam
Ayam

Reputation: 169

Try using the full path (starting from 'C:')

Upvotes: 0

abdoulsn
abdoulsn

Reputation: 1159

Try this way. test = pd.read_csv('tutorial\testData.tsv', header=0, delimiter=r'\t',quoting=3)

Upvotes: 1

CDJB
CDJB

Reputation: 14506

Python is interpreting \t as a tab in the string 'tutorial\testData.tsv'.

You can change this, as you've found out, by using r"..." to indicate it as a raw-string, which means python ignores backslashes.

Upvotes: 3

Related Questions