2 Answers
2

You need to close the file. You can’t easily tell from the number of lines, but I’d expect the size of the file to be 4096 or 8192, which hints that only a whole number of blocks have been flushed. After you call file.close(), the rest of the data should be written.

+1, Context managers are always the best way to deal with files. It might be worth noting that using print in this way is a little odd - and the functionality is different in 3.x - for these reasons, you might want to use file.write() instead, which is clearer.
–
LattySep 20 '12 at 18:40

1

@Lattyware -- using print like this isn't weird at all. It's the same thing as python 3's print(this,that,file=file) as far as I'm aware. The behavior is slightly different than f.write as well. (print automgically calls str on it's arguments and appends a newline unless you tell it not to). That said, I always hated that syntax and would prefer f.write( ... ) as you suggest.
–
mgilsonSep 20 '12 at 18:51

2

@mgilson Weird might be the wrong choice of word - I don't think it's very expressive. As soon as you see file.write(...) you expect writing to a file, print - not so much. I just don't really see the benefit to it here over file.write(str(item)+"\n") - I prefer the explicit option over the slightly less verbose, but that could just be me.
–
LattySep 20 '12 at 18:53

1

@Lattyware -- No, it's not just you. Like I said, I prefer f.write(...) as well (and quite strongly -- I hate that old syntax). My comment was mainly to document the differences between the two methods. (Imagine Op's surprise when trying to convert directly to f.write(item) and it complains because item isn't a string. And then there are no newlines in the file after going to f.write(str(item)) ...
–
mgilsonSep 20 '12 at 19:00

1

@mgilson You make a good point, the differences are important.
–
LattySep 20 '12 at 19:07

CapWords names are usually reserved for classes according to PEP-8. It's also worth noting this is a very inefficient way to produce such a list (a list comp, or, better yet, file.readlines() would be a much better option in a case like this, and will produce a file containing a Python list literal, which isn't what was wanted.
–
LattySep 20 '12 at 18:40

This will require reading the file into memory, which could cause problems with big files or low memory systems.
–
LattySep 20 '12 at 18:46