1

I have read the other answers, but it seems I am still making a mistake somewhere.

I want to process all csv files in a given directory.

def main(): 
    data = []

    for root, dir, files in os.walk('/Users/me/Documents/ssbm csv/ssbm_stats/'):

        for name in files:

            # only csv files
            if name.endswith(".csv"):

                csvpath = os.path.join(root, name)
                c = csv.reader(csvpath)
                print "processing:", csvpath

                games = makeT(c)

It runs but it does the wrong thing. It does not open the csv file using csv.reader().

def makeT(csvfile):
    for row in csvfile:
        print csvfile
        print row
        print len(row)

Output:

<_csv.reader object at 0x10d3ecde0>
['/']
1

The length is wrong. There is no slash character in any part of the csv file; so I think it may be doing something with the filename. I really do not understand why it isn't passing the file properly.

Any idea as how to pass file names to csv.reader()?

0

2 Answers 2

2

You need to pass an actual opened file to csv.reader

with open(csvpath, 'rb') as csvfile:
    c = csv.reader(csvfile)
    ...
1

From the docs

csv.reader(csvfile, dialect='excel', **fmtparams)

Return a reader object which will iterate over lines in the given csvfile. csvfile can be any object which supports the iterator protocol and returns a string each time its next() method is called — file objects and list objects are both suitable.

In your case the first param must be the opened file

It can be wither

csvfile = open(csvpath, 'rb') 
reader = csv.reader(csvfile)

Or

with open(csvpath, 'rb') as csvfile:
    reader = csv.reader(csvfile)

But the second is preferred, because it will automatically close the file.

The other parameters to the csvreader object can include delimeter and quotechar

Not the answer you're looking for? Browse other questions tagged or ask your own question.