469,625 Members | 1,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Simple file line reading

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

Jul 18 '05 #1
2 2667

"Aaron Deskins" <nd******@ecn.purdue.edu> wrote in message
news:40**************@ecn.purdue.edu...
Hello everyone,
I'm relatively new to Python and am trying to write some scripts.
me too :) Started last weekend reading seriously

import string
it = 0
for line in open("OUTCAR"):
it+=1
print it


f = open("OUTCAR")
print(len(f.readlines()))
f.close()

will print the number of lines in the file (I think :) might have made a
typo or something).
--
Jeroen Wenting
jwenting at hornet dot demon dot nl

Jul 18 '05 #2
In article <40**************@ecn.purdue.edu>, nd******@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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Xah Lee | last post: by
38 posts views Thread by jrlen balane | last post: by
7 posts views Thread by dan | last post: by
5 posts views Thread by Rob Somers | last post: by
6 posts views Thread by KevinD | last post: by
27 posts views Thread by one man army | last post: by
26 posts views Thread by jacob navia | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.