I am writing a program that should read in a number of csv files from a directory and do some analysis on each file. I have these functions:
# Function reads in files and saves data into 2 dimensional array
def ReadInFiles(name):
try:
data = []
fname = csv.reader(open(name, 'r'))
#print 'read in file: ' + str(fname)
rowCount = 0
for row in fname:
if rowCount != 0: # skip headings
data.append(row)
rowCount += 1
#print name + ' ' + str(row) + ' read in successfully \n'
except IOError: "Can't read files"
return data
#Function reads in files in a folder
def ReadDirectory (dirName):
try:
Data = []
for files in os.listdir(dirName):
print '\n FILE: ' + files
Data = ReadInFiles(files)
AnalyzeData(Data, daysElapsed, columns = 4)
except IOError:
print 'Directory does not exist!'
return
The weird thing about it is if I ignore the ReadDirectory function and use just the ReadInFile function, it works perfectly (files are read in without any issues) but once I call ReadInFunction from ReadDirectory, I get the error IOError: [Errno2] File or Directory does not exist: logfile.csv
Using os.listdir to list the files in the directory works perfectly too.
I have the folder containing the csv files as a sub directory in the directory that contains the source code. I noticed that if I place copies of the files in the source code directory as well, my scripts works perfectly but if I remove those copies, I start getting the error again.
I have searched previous posts and tried double slashes, / but nothing works. I have checked the permissions on the folder but it doesn’t help either.
Any help will be appreciated!!
Thank you.
P.S. I am fairly new to python so please excuse my possibly bad code
os.listdir()returns bare file names that do not include the name of the directory the files are in.You probably meant:
P.S. Why is
filesplural when it refers to a single file name?P.P.S. The following
exceptblock doesn’t do anything useful and quietly ignores all errors: