0

I'm trying to read multiple csv files in a directory, using pandas. I used two methods, and both are not working.

import os
from glob import glob
from math import*
from numpy import*
from pandas import*


path = '/Volumes/File/Names/Stuff 2016'


for filename in glob(os.path.join(path, '*.csv')):
    qu = read_csv(filename, delimiter = ';', header = 0, skiprows = 24, nrows=2)
    print(qu)

Edit:(The above code works.)
Below when I try to just read all files in that specific path, without specifying it's a csv:

for filename in os.listdir(path):
    q = read_csv(filename, delimiter = ';', header = 0, skiprows = 24,  nrows=2)

FileNotFoundError: File b'STD_20160103.00.csv' does not exist 

This error is confusing me, since that specific file does exist in the directory. I'm wondering if the file names '*.00.csv' are the problem, but I just want to print the values along all the files, and it's not working. Thanks

2
  • Put a print(filename) statement in just before the read_csv and you will see what's wrong. Commented Mar 30, 2018 at 22:34
  • I see, I understand when referring to just filename in the directory, it doesn't give the full extension of the file, so it can't be found. Need to join path and filename, as suggested by answer below. Thanks. Commented Mar 30, 2018 at 22:54

1 Answer 1

2

glob returns the full path ... os.listdir only returns the filenames

so change it to

q = read_csv(os.path.join(path,filename),...
Sign up to request clarification or add additional context in comments.

1 Comment

When I do this, it gives an error in reading the csv. So I go ahead and join path and filename before reading the csv, like I did above; 'for filename in glob(os.path.join(path, '*.csv')):'. This time this works for some reason. But yes I get the idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.