[Tutor] Command Line Arguements in Python

alan.gauld@bt.com alan.gauld@bt.com
Wed, 17 Oct 2001 11:35:25 +0100


> I'm having some trouble reading input and writing output 
> files from the command line as follows:
> 
> python myfile.py < inputfile.txt > outputfile.txt

When you use indirection the files appear in stdin/stdout
- thats what redirection does.

So in your case you read input.txt with raw_input()
You write to output.txt with print.
You don't need to open the files the OS does that 
automatically.

If you would rather have the files as parameters to 
your program then you access them with sys.argv as
described in my online tutor at:

http://www.freenetpages.co.uk/hp/alan.gauld/

Under 'talking to the user'.

To do that you'd call yourt script like:

python myfile.py input.txt output.txt

ie no indirection.
Also in this case you need to use open() and close().

HTH,

Alan G.