Newbie question about file input

Aaron Deskins ndeskins at ecn.purdue.edu
Wed Aug 18 17:43:37 CEST 2004


Grant Edwards wrote:

> I don't understand the question.

Perhaps I should have been more specific. What does python store when 
you read a blank line? Nothing? A null variable? A '\n'?

How about this:

import string
import sys
zf=open(sys.argv[1],'r')
it = 0
while 1:
     line = zf.readline()
     print line
     rs = line.split()
     print rs
     if rs[0]== '[Event':
         it+=1
         print it
     if not line:
         break
zf.close()

This fails when it tries the "if rs[0]== '[Event':" statement. rs[0] 
doesn't exist (or is blank?) for a blank line in my input file.

Another code:

import string
import sys
zf=open(sys.argv[1],'r')
it = 0
while 1:
     line = zf.readline()
     it+=1
     if not line:
         break
zf.close()
print it

This only ends when the end of file is reached. Why not when a blank 
line is read? How does python treat the variable line (after a readline) 
differently after a blank line or the last line of the file?

-- 
Aaron Deskins
Graduate Student
Chemical Engineering
Purdue University



More information about the Python-list mailing list