BBedit
BBedit

Reputation: 8067

How to iterate over files in a directory?

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()?

Upvotes: 1

Views: 111

Answers (2)

Bhargav Rao
Bhargav Rao

Reputation: 52191

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

Upvotes: 1

C.B.
C.B.

Reputation: 8346

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

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

Upvotes: 2

Related Questions