This should be only one call, not three.
exit_status = subprocess.call(
['python3', os.path.expanduser('~/Desktop/mdparser.py')],
stdin=open('input_file', 'r'), stdout=open('output_file', 'w'))
Tilde expansion (~/foo
) is processed by the shell; when you don't have a shell, as here, you need to explicitly do it yourself -- that's what os.path.expanduser
does.
You can't use check_output()
when stdout
is redirected, whether to a different process or a file -- this is why the exception is thrown, as the Python interpreter can't both read the content into a variable and connect it directly into a pipeline to a different process. That's what the message means about "will be overridden" -- when you use check_output()
, you're telling the Python interpreter to read output from a pipeline itself, but it can't do that when you configure that output to go to a different process or a file.
Instead, direct the output straight to the file, and open the file and read it when done.
The other reason not to use cat
is that all it does is add inefficiency and restrict operation. When you run:
foo <input.txt >output.txt
...or, if you prefer the form...
<input.txt foo >output.txt
...the foo
program gets a file handle directly on input.txt
, and another directly on output.txt
. When you don't use cat
, those file handles are the real deal -- it's possible to seek around in the files, meaning that if your program would have to go back and review prior content, it can just tell the file handle to go back and seek to a different part. By contrast, if you ran cat input.txt | foo
, then foo
would have to store everything it read in memory if the operation it's performing requires more than one pass.
Using cat
is just overhead here -- it's an extra program that reads from the input file and writes to its half of the pipeline, after all, meaning that it's doing extra IO to and from the pipe and context switches to and from the kernel. Don't use it unless you need to -- such as if you're concatenating multiple files into a single stream (which is cat
's purpose, hence its name).