mdandr
mdandr

Reputation: 1384

How do you write CSV files to a specific folder that you've recently created in python?

This is what I currently have:

CSVdir = "./CSV_folder"
realCSVdir = os.path.realpath(CSVdir)
if not os.path.exists(CSVdir):
    os.makedirs(CSVdir)
writer = csv.writer(open(realCSVdir + 'Movie-%s.csv' % var, 'wb'))

I'm trying to write my CSV files in the CSVdir that was just created, but it's not working.

Upvotes: 1

Views: 2401

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169284

The problem is that you've left out the path separator between realCSVdir and the filename.

Use os.path.join for a cross-platform solution:

# this is meant to follow on from the `os.makedirs` line...
foupath = os.path.join(realCSVdir, 'Movie-%s.csv' % var)
fou = open(foupath, 'wb')
writer = csv.writer(fou)

Upvotes: 2

Related Questions