Simple file line reading

Bill Rubenstein wsr2 at swbell.net
Sat Mar 13 09:19:43 EST 2004


In article <40520BD5.9090006 at ecn.purdue.edu>, ndeskins at ecn.purdue.edu says...
> Hello everyone,
>    I'm relatively new to Python and am trying to write some scripts. 
> I've written a program (with the help of a friend) to search a text file 
> line by line for a certain section of information and then process some 
> of that information. This was on machines with Python 2.2.1 and 2.3.2.
>    When I try the same program on another machine using Python 2.1.2 and 
> another with Python 1.5.2, the script does not work. I've isolated the 
> part of the code giving me problems below. The code below should just 
> print out a number for every line. The actual code has some other 
> commands to run at each line of the code. I just want to read each line 
> of the file, run some commands on the contents of that line, and stop 
> when the end of the file is reached.
> 
> 
> import string
> it = 0
> for line in open("OUTCAR"):
>    it+=1
>    print it
> 
> The error message is:
> 
> Traceback (most recent call last):
>    File "testr.py", line 3, in ?
>      for line in open("OUTCAR"):
> TypeError: loop over non-sequence
> 
> Thanks for your help.
> 
> 
 The ability to iterate on a file object (returned by open()) I think is new at 
2.3.

Bill





More information about the Python-list mailing list