0

How can I print some part of a returned stdout of a subprocess? E.g. From the example below, how can I print only the "Number of features" and the "Number of predicates" ?

command = "java -cp \"C:\mallet-2.0.7\class;C:\mallet-2.0.7\lib\mallet-deps.jar\" cc.mallet.fst.SimpleTagger --train true --model-file model train
p = Popen(command, stdout=PIPE, stderr=STDOUT, shell = True)   
for line in p.stdout:
    print line

>>>
   Number of features in training data: 6

   Number of predicates: 6

   Labels: O noun non-noun

   Preparing O

   O->O(O) O,O

   ......... 
DevEx
  • 4,337
  • 13
  • 46
  • 68
  • This has nothing to do with subprocess really. You need to parse the output (re maybe) and get the things you need. – Jayanth Koushik Mar 05 '14 at 11:34
  • 1
    You might want to consider changing your `command = "` to `command = '` or `command = """` so that you don't have to escape those `"`. – sotapme Mar 05 '14 at 12:24
  • Also, use a raw string to avoid backslashes being misinterpreted: `command = r'java -cp "C:\mallet-2.0.7\class;C:\mallet-2.0.7\lib\mallet-deps.jar" cc.mallet.fst.SimpleTagger --train true --model-file model train'`. In this case it doesn't hurt, but had a directory begun with (e.g.) `t` or `n` there'd be a tab or newline in your file name! – holdenweb Jul 05 '19 at 10:47
  • Avoiding `shell=True` altogether would be better still. `command=['java', '-cp', r"C:\mallet-2.0.7\class;C:\mallet-2.0.7\lib\mallet-deps.jar", 'cc.mallet.fst.SimpleTagger', '--train', 'true', '--model-file', 'model', 'train']`; see also [Actual meaning of `shell=True` in `subprocess`](/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee Jul 05 '19 at 12:00
  • You should probably prefer `subprocess.run()` or old-style `subprocess.check_output()` over bare low-level `subprocess.Popen()`; see also https://stackoverflow.com/questions/4256107/running-bash-commands-in-python/51950538#51950538 – tripleee Jul 05 '19 at 12:03

1 Answers1

0

Well the easiest way I can see is to change your last piece of code to:

for line in p.stdout:
    if "Number of" in line:
        print line

It's only data, you just need to select the bits you want (or need).

EDIT: Note this approach is somewhat simplistic. If the child process were to create so much output on stderr that buffers filled up it would stall unaccountably. Sending stderr to a file, even /dev/null, avoids this possibility.

holdenweb
  • 33,305
  • 7
  • 57
  • 77