andrejr
andrejr

Reputation: 159

Python glob module

How do I read all files in a directory that end in *.csv or .bck using glob, then print them all to one file?

At the moment I have the following:

import glob

path1 = "C:\Temp\eqtests\*.csv"
path2 = "C:\Temp\eqtests\*.bck"

with open('C:\Temp\output.csv', 'wb') as outf:
writer = csv.writer(outf)
   for fname in glob.glob(path1) and glob.glob(path2):
   with open(fname,'rb') as inf:
      for row in reader:
          writer.writerow(row)

Upvotes: 0

Views: 341

Answers (1)

skrrgwasme
skrrgwasme

Reputation: 9633

As suggested in comments:

for fi_name in glob.glob(path1) + glob.glob(path2):

This will take the two lists returned by the two calls to glob.glob(), add them together, and iterate over the resulting list. You current code didn't work because you were using the and statement, which is a boolean operator.

Upvotes: 1

Related Questions