iterating over lines in a file

David Bolen db3l at fitlinxx.com
Sun Jul 23 19:28:33 EDT 2000


nobody <no at bo.dy> writes:

> perhaps. unfortunately, i haven't had time to work on this since i made
> that posting, so i have got no further; i expect there's some fairly easy
> way to write (copy) all the lines from one file to another one, possibly
> (though not necessarily) stdout. perl is nice in that this task is a one-
> liner in that language; i don't *need* it that concise, but it would be
> nice. i like one-liners.

As long as you're willing to think in terms of "lines" (which does
technically involve some "cleverness" or intelligence on the I/O
part), then this sort of thing is probably the most straight forward,
assuming stdin->stdout:

    import sys

    while 1:
	line = sys.stdin.readline()
	if not line: break
	sys.stdout.write(line)

The overhead compared to the Perl example is just about the same as
just processing input (e.g., the "while 1" idiom - the extra operation
to dump to stdout is only one line, just as with Perl), or if you
switch to fileinput:

    import fileinput

    for line in fileinput.input():
        sys.stdout.write(line)

which aside from the import, is pretty much as brief as the Perl
approach :-)  To be honest though, while I'm as much a fan of brevity
and one-liners as the next guy, they do carry some risk sometimes in
terms of encapsulating implicit behavior that can sometimes make the
code less readable.

Now, this does assume a line formatted setup, which I think is also
assumed by the Perl example.  Both the "while <FILE>" in Perl, and the
"readline" in Python read up until the end of line, and include the
newline in the returned string.  So there is some non-raw processing
going on.  Just as with "print" under Perl, Python's "write()"
file method just dumps the contents of the string to the file, any
newline already in the string included. 

But if you really wanted something closer to a raw copy, with no
assumptions about a format of lines, then you could switch to using a
read() method rather than readline(), at least in the first approach
above.

> that's trying to be clever. then, if i'm reading raw lines from a file and
> want to print them verbatim, i have to strip a newline somewhere somehow?
> how do i copy a file to another one, is there a file.copy method in some
> module somewhere?

BTW, I do agree that "print" has a number of clever items to it as a
statement, and that's fine - it's how it is defined, and it's very
convenient interactively and even in scripts.  It just doesn't match
the definition of Perl's "print", despite the same name.

The file write() method is the closest match to Perl's "print FILE"
statement - just use "sys.stdout.write()" where you'd leave off FILE
in Perl.  If you don't like the length of the name, just assign it to
any name you'd like in your script and use that instead :-)

Where a typical incorporation of variables into Perl's print statement
is just embedding $variable in the string, with Python you would use
the string formatting operations to build up the appropriate string to
send to write(), say something like ("%s is %d" % (var1,var2)).  If
you use the dictionary approach ("%(name)") then it doesn't even read
all that differently from Perl :-)

Just to note too - you mention reading "raw lines" - that's sort of
oxymoronic, since by definition "lines" have format information (end
of line markers that differ by platform) that have to be processed,
and thus you aren't really reading anything "raw" :-)

Oh BTW, if you really just wanted a file copy, then yes, you could
also use the higher level copy() method in the shutil module.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list