0

I am trying to do a complexity analysis of the stanford parser. To do so, I am starting the program via a cmd file, so therefore if I use subprocess.check_output, my python program will give me the commandline arguments I am using. The parser prints its own runtime on the commandline, so therefore I have to actually come up with something which reads out what the program I have started printed on the commandline.

subprocess.check_output("path-to-cmd", shell=True tldr: This gives me the cmd-files output, I want what the started program printed in the terminal.

As my question was marked as a duplicate, I want the output of a program that I have started via the cmd, if I use subproces.check_output, it will simply give me the content of my cmd, and not the output of the java program I have run. I want to capture what the java program wrote to the terminal.

Tom
  • 1
  • 1
  • The obvious thing to do here is to edit the cmd file so whatever extra output it's printing doesn't get printed, or gets printed to stderr instead of stdout. Or, if the cmd is simple enough, just scrap it and call the program directly, without the shell. – abarnert Jun 25 '18 at 18:06
  • 1
    Possible duplicate of [Running shell command from Python and capturing the output](https://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output) – sudonym Jun 25 '18 at 18:09
  • You may find this [answer](https://stackoverflow.com/a/4416529/355230) of mine useful. – martineau Jun 25 '18 at 18:45

1 Answers1

0
import subprocess

# Create test file.
with open('test.cmd', 'w') as w:
    w.write(
     '@echo off\n'
     'echo 1 stdout\n'
     '>&2 echo 2 stderr\n'
     '>&3 echo 3 program output\n')

output = subprocess.check_output(
 'test.cmd 1>nul',
 universal_newlines=True,
 shell=True)

print('check_output:', repr(output))

The example will get the programs output from handle 3. The program here is just an echo to mimic a program though the redirection is the goal.

CMD supports up to 9 output handles as quoted from the SS64 site:

STDIN  = 0  Keyboard input
STDOUT = 1  Text output
STDERR = 2  Error text output
UNDEFINED = 3-9

You can output programs to handle 3 in the batch file. Then you can redirect handle 1 to nul i.e. 1>nul or just >nul in the Python file. Thus, check_output will only output handle 3 as the stdout.

Output:

2 stderr
check_output: '3 program output\n'
  • Output uses repr() to show output in 1 line for testing.
  • No output of the line 1 stdout as handle 1 was redirected to nul.
  • Stderr will still print out to console as it is not redirected. You can choose how to handle stderr.

If the Stanford Parser outputs the data as stderr (handle 2) instead of stdout (handle 1), then you may use 2>&3 in the batch file command to redirect to handle 3. i.e.

2>&3 java -cp stanford-parser.jar ...

I have no experience with the Stanford Parser so the command example is a guess from online examples from stanford.edu.

If you want all of the output instead of just program output and the program outputs to handle 2. Then use in the check_output with 2>&1 or the recommended argument stderr=subprocess.STDOUT and omit the 1>nul. This may include batch file script errors which may be undesirable.

If possible, rewrite the batch file to Python as you avoid complication and 1 script gets all the control.

michael_heath
  • 5,262
  • 2
  • 12
  • 22