Output file formatting/loop problems -- HELP?

Hendrik van Rooyen hendrik at microcorp.co.za
Tue Sep 8 18:05:30 CEST 2009

On Tuesday 08 September 2009 17:22:30 Maggie wrote:
> My code is supposed to enumerate each line of file (1, 2, 3...) and
> write the new version into the output file --
> #!/usr/bin/python
> import os.path
> import csv
> import sys
> #name of output file
> filename = "OUTPUT.txt"
> #open the file
> test = open ("test.txt", "r")

After this, do the following and see what you get:

for i,line in enumerate(test.readlines()):
  print i, line

> my question is why the enumeration starts and stops at first line and
> doesnt go through the entire file --

It does - but it sees the entire file as one line, somehow.

> (file is saved as .txt, so hypothetically no .rtf formatting that
> would screw up the output should be present)

If it is really text, and if there are newlines at the end of the lines, then 
it should JustWork... 

- Hendrik

More information about the Python-list mailing list