testing true condition

Lars von Wedel lvw at lfpt.rwth-aachen.de
Wed Apr 19 07:20:49 EDT 2000


Hi,

You can do something like this if you want an easier way to iterate
over a number of lines of a number of files

import fileinput

for line in fileinput.input():
    print line,

This statement iterates over all lines of all files submitted via the
command line. Look into the documentation for module fileinput, it
offers a number of other interesting stuff. You could e.g. call

 test.py *.txt > concat.txt

to generate a concatenated list of all .txt files with the above.

Don't forget the ',' at the end of the print statement. Otherwise
each line will have a blank line after it when you read text files.

Lars



Remco Gerlich writes:
 > Gregoire Welraeds wrote in comp.lang.python:
 > > I know that I can't do something like the following:
 > > 
 > > >>> while (line= f.readline()):
 > > 	...
 > > 
 > > as i could in C.  Is there any replacement tricks ?
 > 
 > The standard Python idiom is:
 > 
 > while 1:
 >    line = f.readline()
 >    if not line:
 >       break
 > 
 > There are also wrapper classes around file input, so you can use it in a for
 > loop, but it doesn't read in the whole file at once (same difference as with
 > range and xrange). Don't remember its name, but it's not hard to roll your
 > own.
 > 
 > -- 
 > Remco Gerlich,  scarblac at pino.selwerd.nl
 >   Murphy's Rules, "Try a firecracker":
 >    In TSR's Top Secret, it usually takes three grenade explosions to kill
 >    the average player-character-agent.
 > -- 
 > http://www.python.org/mailman/listinfo/python-list
 > 





More information about the Python-list mailing list