[Tutor] reading stdin?

Kalle Svensson kalle@gnupung.net
Fri, 16 Feb 2001 20:52:12 +0100


--1yeeQ81UyVL57Vl7
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Sez D-Man:
> On Fri, Feb 16, 2001 at 01:17:32PM -0500, Mr 804 wrote:
> |=20
> | how do I read stdin?
> |=20
[snip great introduction to input/raw_input]

If you want to read from standard input in a more low-level way, perhaps
better suited for redirected input and that kind of stuff, use sys.stdin.

import sys
# get input
s =3D sys.stdin.read()
# do stuff

sys.{stdin|stdout|stderr} are ordinary file objects.  This makes it easy to
use standard input/output as defaults, e.g. if no input/output files are
specified on the command line:

# crude example
import sys
if len(sys.argv) =3D=3D 1:
    outfile =3D sys.stdout
    infile =3D sys.stdin
elif len(sys.argv) =3D=3D 2:
    outfile =3D open(sys.argv[1])
    infile =3D sys.stdin
else:
    outfile =3D open(sys.argv[1])
    infile =3D open(sys.argv[2])

HTH,
  Kalle
--=20
Email: kalle@gnupung.net     | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD

--1yeeQ81UyVL57Vl7
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE6jYTsdNeA1787sd0RAsbmAKCPalN1+ULLJGql/BIT1e5R8tY7fwCgqZlO
/U7UlVZLhKK+CmI+WDyx6Gg=
=GPzc
-----END PGP SIGNATURE-----

--1yeeQ81UyVL57Vl7--