[Tutor] Command Line Arguements in Python

Roeland Rengelink r.b.rigilink@chello.nl
Wed, 17 Oct 2001 08:00:37 +0200

Mike Yuen wrote:
> I'm having some trouble reading input and writing output files from the
> command line as follows:
> python myfile.py < inputfile.txt > outputfile.txt
> The problem is, I don't know how to get access to the inputfile.txt and
> outputfile.txt inside my code.  Normally, i'm used to using the "open"
> command but I don't want to hard code it in anymore.  FYI, i'm using Python
> on a UNIX O/S.
> Thanks,
> M

Hi Mike,

Danny already told you about sys.stdin and sys.stdout

You can also simply use (raw_)input() and print, which read from stdin
and write to stdout respectively.

Remember, inputfile.txt and outputfile.txt are not command line
arguments. '>' and '<'
are redirection operators which tell the OS to connect inputfile.txt
(rather than the keybord) to the standard  input stream and to connect
outputfile.txt (rather than the terminal) to the output stream of a

Hope this helps,


"Half of what I say is nonsense. Unfortunately I don't know which half"