1

For some reason it isn't writing to the CSV. Can anyone see why it wouldn't write?

def main():
    list_of_emails = read_email_csv() #read input file, create list of emails
    master_list_writer = open_master_list() #open output file so we can write to it
    .....
    write_to_csv(master_list_writer, list_of_url, linkedin_information, facebook_information, twitter_information)

def open_master_list():
    final_csv = open('ruby_dev_final1.csv', 'wb')
    output_writer = csv.writer(final_csv)
    return output_writer

def write_to_csv(master_list_writer, list_of_urls, linkedin_information, facebook_information, twitter_information): #add in linkedin_list
master_list_writer.writerow(list_of_urls + linkedin_information + facebook_information + twitter_information)
  print list_of_urls, linkedin_information, facebook_information, twitter_information
  print 'is this working?'
  return

This is the print out:

['jim.weirich@gmail.com', u'Cincinnati Area', u'http://www.facebook.com/jimweirich', u'http://www.linkedin.com/in/jimweirich', u'http://twitter.com/jimweirich'] ['Jim', 'Weirich', 'Chief Scientist', 'Neo', 'Cincinnati Area', 'P1Y10M', 'Present'] ['', '', ''] ['', '', '', '']
is this working?
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Morgan Allen
  • 3,291
  • 8
  • 62
  • 86
  • 1
    Your code is definitely writing the CSV, but I guess you don't know where the *directory* is where it is written. Add a `print os.getcwd()` to your debug output too, then look in *that* location. – Martijn Pieters Feb 28 '14 at 18:31
  • in write_to_csv, you need to call master_list_writer.close() – Ryan G Feb 28 '14 at 18:32
  • 1
    @RyanG: closing is optional; it happens automatically when the script exits. – Martijn Pieters Feb 28 '14 at 18:32
  • @MartijnPieters - Good to know. I just remembered always having blank files when I didn't close the steam back when I started learning python. So it's become a force of habit :D – Ryan G Feb 28 '14 at 18:40
  • @RyanG im iterating through 1,000's of rows, so wouldn't opening and closing it cause me to overwrite the CSV file? – Morgan Allen Feb 28 '14 at 18:44
  • @MorganAllen - Yes, unless there is somewhere better to place the close() after everything is finished, you may be able to use mode "a" instead of "wb". I'm unsure if "ab" exists of the top of my head. – Ryan G Feb 28 '14 at 18:46

2 Answers2

2

Your code is definitely writing the CSV, but I guess you don't know where the directory is where it is written.

Add a print os.getcwd() to your debug output too, then look in that location.

Or better still, use an absolute path to open the CSV file to ensure it is written to a known location instead. You could use the filename of the script as a start:

import os.path

base = os.path.basename(os.path.abspath(__file__))

def open_master_list():
    final_csv = open(os.path.join(base, 'ruby_dev_final1.csv'), 'wb')
    output_writer = csv.writer(final_csv)
    return output_writer

Now your CSV file will always be created in the same directory as the script.

If your script never exits, on Windows you won't see the data written until you close the file; you'd need to store a reference to final_csv somewhere so you can call close() on that object (you cannot retrieve it from the csv.writer() object).

On most platforms, there is also a buffer; if you write only a few rows, you may not see data until the buffer has been flushed (normally this happens on close), you'll need the final_csv reference still and call final_csv.flush() to flush the buffer to disk.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Hey Martin, I can find the file, but it's always empty. I have the script on my desktop and that is where the file is being created. – Morgan Allen Feb 28 '14 at 18:38
  • @MorganAllen: Does your script ever exit? – Martijn Pieters Feb 28 '14 at 18:39
  • Ah Hah! So I'm not crazy! :P – Ryan G Feb 28 '14 at 18:41
  • Hah, no :-) ... I've gotten this to work before like 3 months ago and haven't touched it since, but for some reason it just stopped working...I wasn't sure if edits were made to csv writer or something? i normally let the script run a few times then kill it and check the file – Morgan Allen Feb 28 '14 at 18:43
  • @MorganAllen: No, `csv.writer()` hasn't changed, and unless your system was updated to a newer Python version you'd not see changes anyway. :-) – Martijn Pieters Feb 28 '14 at 18:45
0

There are multiple answers to this already, but to summarize;

You need to call close on the file object, or use with

See

Community
  • 1
  • 1
jmetz
  • 12,144
  • 3
  • 30
  • 41