[Tutor] file I/O

steve lonetwin@yahoo.com
Fri, 13 Jul 2001 13:13:26 +0530


Hi there,
 Thanx Danny an' Remco, well, I kinda see what U mean, though I now have=20
to figure out when using fileinput would be a *GOOD* thing...but :)
I guess, (like it's said) when the problem comes, there shall be one obvi=
ous way
to solve it :)

Thanx again
Steve

On Thursday 12 July 2001 12:18, you wrote:
> On  0, steve <lonetwin@yahoo.com> wrote:
> > =09I've been meaning to ask this question for a long time now,
> > I've always seen ppl do file i/o, especially file read using an open(=
)
> > followed by a loop of readline(), stripping the '\n' at the end....
> >   I discovered fileinput (the module) quite early on when I started
> > learning python....so I always do things like
> >
> > Python 2.1 (#3, Jun 25 2001, 13:39:27)
> > [GCC 2.95.3 19991030 (prerelease)] on linux2
> > Type "copyright", "credits" or "license" for more information.
> >
> > >>> import fileinput
> > >>> p =3D [ line.strip() for line in fileinput.input("myfile.txt") ]
> >
> > =09What bothers me is that I don't see enough ppl do that, an' I wond=
er is
> > it b'cos it is some kinda *Bad thing* ....could n e one comment on th=
is
> > ???

[ Remco ] :

>
> Well, not a bad thing, but I think you lose most of the advantages of
> fileinput (not reading in the whole file at once, for one). On the othe=
r
> hand, fileinput will probably give a small performance hit because it h=
as
> to supply these things, and the strip() in the list comprehension are m=
inor
> slowdowns as well.
>
> If you need the whole file in lines, but without \n, the easiest way is=
 to
> read it all in and to split on \n:
>
> p =3D open("myfile.txt","r").read().split("\n")
>

[ Danny ] :

The fileinput module itself isn't bad: if you have familiarity with the
Perl programming language, you might recognize fileinput as an analog to
the magic '<>' operator.  When using fileinput.input(), we can actually
leave it's input argument blank.  To show this, let's make a fast
'one-liner' program.

###
import fileinput; print ''.join([l.strip() for l in fileinput.input()])
###

This one-liner program takes any text file, and turns it into one long
line.  As we can see, there's no filename involved: fileinput()'s
usefulness comes from the fact that it will automagically look for its
input from filenames given at the prompt or standard input.  Here are a
few sample runs:


###
[dyoo@tesuque dyoo]$ python makeOneLiner.py=20
hello world
this is a test
of the emergency broadcast
system.
                  [At this point, I press Ctrl-d a few times to indicate
                   the end of input.]
hello worldthis is a testof the emergency broadcastsystem.


[dyoo@tesuque dyoo]$ cat foo.txt
hello again
This is a another test of the emergency
broadcast system.
[dyoo@tesuque dyoo]$ python makeOneLiner.py foo.txt
hello againThis is a another test of the emergencybroadcast system.
###


This obscure program makes everything hard to read.


--=20
||||||||||||||||||||||
|||||||||#####||||||||
||||||||#######|||||||
||||||||# O O #|||||||
||||||||#\ ~ /#|||||||
||||||##||\_/||##|||||
|||||#||||||||||##||||
||||#||||||||||||##|||
||||#|||||||||||||##||=09
|||/\##|||||||||##/\||=09
|/    \#########/    \=09
|\     \#######/     /=09
||\____/#######\____/|=09
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=09
Debug is human, de-fix divine.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D