newbie question...
Amit Patel
amitp at Xenon.Stanford.EDU
Tue Dec 28 22:08:35 EST 1999
Alexander Sendzimir <sendzimir at earthlink.net> wrote:
| As a developer new to the Python language and experienced with the Perl language
| (and quite a few others ;-), I'm wondering what the accepted method of handling
| a text file's contents are in Python. For example, the following Perl construct
| is pretty standard.
|
| while ( <SOMEFILEHANDLE> )
| {
| # process each line as if comes off the file handle...
| }
|
| is typical.
|
| The equivalent Python appears to be
|
| somefilehandle = open( "some/file/name.text" )
| all_the_lines_in_the_file = somefilehandle.readlines()
| somefilehandle.close()
|
| # now process the lines in the all_the_lines_... list
| # using some prefered method (there's more than
| # one way of doing this, of course ;-)
|
I tend to use:
import fileinput
for line in fileinput.input():
# do something with the line
I'm not sure if you can use this for a file handle; it may only work
for filenames. :-(
- Amit
More information about the Python-list
mailing list