1

I am writing to csv in python, and I was wondering why my output files are blank when the program completes running. First off, here is my code:

reader = csv.reader(open(location, 'rU'), delimiter = ',', quotechar = '"') writer = csv.writer(open('temp.csv', 'wb'), delimiter = ',', quotechar = '"') writer.writerow(["test", "test", "test", "test", "test", "test", "test"]) count = 0 for row in reader: if count != 0 and count < len(split): writer.writerow([row[0], row[1], row[2], row[3], row[4], row[5], split[count-1]]) count = count + 1 

I know a few things about it:

  • When the program finishes, all that is written to 'temp.csv' is the "test" line. The rest of the document is empty.
  • If I force stop the program in the middle of running, there is in fact text being written to the file: it just gets wiped for some reason.
  • I've done some research, and lots of people have found that "closing" the csv file solves their problems, but that doesn't seem to work for me, unless I am missing something obvious!

Thanks in advance for your help!

Edit: here is some updated code I am working with:

infile = open(location, 'rU') outfile = open('temp.csv', 'wb') reader = csv.reader(infile, delimiter = ',', quotechar = '"') writer = csv.writer(outfile, delimiter = ',', quotechar = '"') split = email_text.split('NEW MESSAGE BEGINS HERE') count = 0 for row in reader: if count != 0 and count < len(split): writer.writerow([row[0], row[1], row[2], row[3], row[4], row[5], split[count-1]]) outfile.flush() count = count + 1 infile.close() outfile.close() 
3
  • 1
    How would you close the file? You're not saving a file handle on which you could use the .close() method. Commented Jul 12, 2012 at 14:52
  • Aside: instead of manually incrementing count, you could use for count, row in enumerate(reader):. Commented Jul 12, 2012 at 14:54
  • @brian: I just had a crazy idea. Could you add a print count statement inside your loop and confirm that count is actually going up? Commented Jul 12, 2012 at 16:46

2 Answers 2

5

You're not closing the file. While this may work fine if you're just reading a file, it's definitely not a good idea to do this when writing a file.

Use a context manager:

with open(location, 'rU') as infile, open('temp.csv', 'wb') as outfile: reader = csv.reader(infile, delimiter = ',', quotechar = '"') writer = csv.writer(outfile, delimiter = ',', quotechar = '"') ... 

This ensures that the files will be closed when the with block is exited (even if it's because of an exception).

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, I tried that, and i got an "Invalid syntax" error at the "with open(location, 'rU')" bit. Sorry, I'm new to python!
@brian: You must be using an old version of Python. with is available since Python 2.6 (and on Python 2.5 if you start your script with from __future__ import with_statement).
I'm using python 2.4.3, but I'm on a network, so I don't have privileges to install or update anything. Is there any way to read these in 2.4.3?
Whoa. How old is Python 2.4 again? Over 6 years? OK, well then no with statement for you. But you can still do infile = open(...) and outfile = open(...), then do reader = csv.reader(infile,...) etc., and finally call infile.close() and especially outfile.close().
Okay, I did this, and it still didn't leave me with anything in my outfile! I'm not sure what the problem is...
|
1

Change the first two lines to save the opened file handles, and then include close invocations afterwards

reader_file = open(location, 'rU') writer_file = open('temp.csv', 'wb') reader = csv.reader(reader_file, delimiter = ',', quotechar = '"') writer = csv.writer(writer_file, delimiter = ',', quotechar = '"') # ... rest of code ... reader_file.close() writer_file.close() 

2 Comments

This didn't work for me, I am still left with an empty outfile!
There might be an issue with your if statement. Else, did you intend for the writer_file to be opened in binary mode?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.