I would like to go through different csv files contained in different folders in the same directory. My folders are in my working directory. My folders are named:
folder1, folder2,folder3
each of them have csv's with identical names csv1.csv, csv2.csv.
I tried this code:
import os
import re
import pandas as pd
from pandas.core.frame import DataFrame
rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir, topdown=False):
print('Found directory: %s' % dirName)
for fname in fileList:
print('\t%s' % fname)
if "csv1.csv" == fname:
var= pd.read_csv(fname)
I can print the name of the csv file in that folder but i get an error:
IOError: File csv1.csv does not exist
what could be the problem?
pd.read_csv(os.join(path, fname))/home/userand your files are located in/home/user/dir1, /home/user/dir2..., you need to at least have the relative path of the filedir2/csv1.csv.