trouble writing results to files

Roberto Bonvallet Roberto.Bonvallet at
Wed Nov 29 16:22:57 CET 2006

lisa.engblom at wrote:
> import csv
> output = csv.writer(open('/Python25/working/output.csv', 'a'))
> a = ["apple", "cranberry", "tart"]
> for elem in range(len(a)):
>    output.writerow(a[elem])

output.writerow expects a sequence as an argument.  You are passing a
string, which is a sequence of characters.  By the way, what output are you
expecting to get?  Do you want a file with only one line (apple,
cranberry, tart), or each fruit in a different line?

BTW, iterating over range(len(a)) is an anti-pattern in Python.  You should
do it like this:

    for item in a:

> Second, there is a significant delay (5-10 minutes) between when the
> program finishes running and when the text actually appears in the
> file.

Try closing the file explicitly.
Roberto Bonvallet

More information about the Python-list mailing list