tags:

views:

52

answers:

4

Hello, Python newbie here. I was trying to troubleshoot an issue with writing a csv file in a larger program and decided to go back to basics to try to find the problem.

I ran an exact code example from the Python csv reading and writing documention:

import csv     
spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ', quotechar='|')    
spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

When I go to my working directory and click on "eggs.csv" the file is empty is reported as being "0 kb". This same thing was happening in my larger program (empty csv files). Am I missing something completely obvious?

Thank you!

EDIT I just tried modified the code to:

import csv

csvOut=open("eggs.csv", "wb")

spamWriter = csv.writer(csvOut, delimiter=' ', quotechar='|')

spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])

spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

csvOut.close()

And this worked. I am not sure why the first doesn't work for me.

A: 

That code works. Are you sure your OS isn't just rounding down the CSV size -- after all, it will only be a few bytes!

You can try print(open("eggs.csv").read()) to see whether the file is actually empty, or else more eggs.csv from the command line.

katrielalex
That is definitely a possibility. But it's more that when I open "eggs.csv" in notepad there is nothing there.
Mina
@Amanda: In that case it's definitely empty. Are you sure you're checking the _correct_ `eggs.csv`? Maybe you're not working in the directory you thought you were... try `os.getcwd()`.
katrielalex
A: 

The code you posted works perfectly for me.

Are you sure that the directory in which you run that script is actually the directory you're checking? (Do you delete "eggs.gsv" every time before you try it?)

detly
Hm, yes I just deleted it and tried again. Same thing
Mina
Actually when I just tried to delete AGAIN it said the file couldn't be deleted because it was in use by another program. This gave me the idea to try to the edit above
Mina
+1  A: 

I'm not too familiar with the csv module, but this does look like a file IO problem more than a csv problem.

The reason that you see nothing in the file is that python still has the file open. You need to close it.

So rather than doing this:

spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ', quotechar='|')

Do this instead:

f = open('eggs.csv', 'w')
spamWriter = csv.writer(f, delimiter=' ', quotechar='|')
# the rest of your code
f.close()

Now you should see what you want in eggs.csv

Hope this helps

inspectorG4dget
Yep, looks like we came up with the solution simultaneously! However, I still find it odd that for some it works w/out the "f.close()".
Mina
I think that those for whom this works without the "f.close()" are using "with" and "as". If you have an example from such people, then I could take a look and analyze it for you.
inspectorG4dget
A: 

I tried this out in the python interpreter. In the first bit of code, I see content in the csv only after I exit python (when the file is closed). Definitely, this was something to with closing the file.

Nigel