Simple file line reading

Aaron Deskins ndeskins at ecn.purdue.edu
Fri Mar 12 14:13:25 EST 2004


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.

-- 
Aaron Deskins
Graduate Student
Chemical Engineering
Purdue University




More information about the Python-list mailing list