The common placeholder for stdin
or stdout
is -
:
./myscript.py - - volumeid
and:
if filename == '-':
input_file = sys.stdin
else:
input_file = open(filename, 'rU')
etc.
In addition, you could default filename
and filename_out
to -
when there are fewer than 3 command line arguments. You should consider using a dedicated command-line argument parser such as argparse
, which can handle these cases for you, including defaulting to stdin
and stdout
, and using -
.
As a side note, I'd not use print
to write to a file; I'd just use:
file_new.write(x)
which removes the need to strip off the newlines as well.
You appear to read from the input file twice; once to parse the XML tree, once again to call change_class()
with the open file object. What are you trying to do there? You'll have problems replicating that with sys.stdin
as you cannot re-read the data from a stream the way you can from a file on disk.
You'd have to read all the data into memory first, then parse the XML from it, then read it it again for change_class()
. It'd be better if you used the parsed XML tree for this instead, if possible (e.g. read the file only once, then use the parsed structure from there on out).