0

When using the popen to unix commands, i get certain outputs to the console.

I understand that subprocess.popen has features that supresses the output. Suppress output from subprocess.Popen

Does os.popen have the same supression feature?

please note that I've pipes in my unix command and i can't get subprocess.Popen to work. My command looks like this

$ echo "es ist also grotesk , wenn herr jospin die europäische richtlinie und die britische datumsgestützte ausfuhrregelung gröblichst mißachtet und durch seine eigenen bedingungen zu ersetzen trachtet ." | treetagger/cmd/tree-tagger-german-utf8

Community
  • 1
  • 1
alvas
  • 115,346
  • 109
  • 446
  • 738

3 Answers3

2

Do not use pipes, do not use os.popen:

import subprocess

text = "es ist also ..."
tt = subprocess.Popen('treetagger/cmd/tree-tagger-german-utf8', 
                      stdout=subprocess.PIPE, 
                      stderr=subprocess.PIPE,
                      stdin=subprocess.PIPE)
tagged, stderr = tt.communicate(txt)

Back in the day when I was the processing pipeline developer for the Text+Berg corpus, I wrote a wrapper class for the TreeTagger, circumventing the wrapper scripts (which are of dubious quality anyway). Unfortunately, the code is proprietary.

Torsten Marek
  • 83,780
  • 21
  • 91
  • 98
  • my wrapper did very crazily simple things to get the lemmas =) http://pastebin.com/2Qnz4yFc – alvas Mar 20 '13 at 06:30
  • Contact me (see my profile) and I can get you in touch with the people that own this code, they might give it to you. – Torsten Marek Mar 20 '13 at 06:53
  • Why shouldn't I use "pipes" and `os.popen`? What's the difference between `os.popen` and `subprocess.Popen`? – maciejwww Feb 10 '23 at 14:18
1

Just use subprocess.check_output instead. It will capture the output in a string for you, and will also do error checking automatically (throwing an error if the command fails).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • it's sort of weird, i want the outputs in strings but not the error output. how can i do that? – alvas Mar 20 '13 at 04:20
  • See my answer. You can have the result in string. – pynovice Mar 20 '13 at 04:21
  • 1
    I think `check_output` does put the command's stdout in the result string but leaves the command's stderr hooked up to the calling script's stderr. Is that not what you want? – John Zwinck Mar 20 '13 at 04:23
  • i think it's not working due to the pipes, my command looks like this `echo "es ist also grotesk , wenn herr jospin die europäische richtlinie und die britische datumsgestützte ausfuhrregelung gröblichst mißachtet und durch seine eigenen bedingungen zu ersetzen trachtet ." | treetagger/cmd/tree-tagger-german-utf8` – alvas Mar 20 '13 at 04:37
0

Do like this:

result = subprocess.Popen(['command'], 
           stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]  

More information can be found in the documentation.

pynovice
  • 7,424
  • 25
  • 69
  • 109