User Tools

Site Tools


doc:appunti:prog:python_program_execution

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
doc:appunti:prog:python_program_execution [2011/05/13 10:18] – [Get the output of a program] niccolodoc:appunti:prog:python_program_execution [2017/07/16 06:44] – [Run two commands in a pipe] niccolo
Line 17: Line 17:
 </code> </code>
  
-Same as above, but run in a subshell:+Same as above, but run in a subshell (in this case the command may contain shell redirections):
  
 <code python> <code python>
-retcode = call("mycmd myarg", shell=True)+retcode = subprocess.call("mycmd myarg", shell=True)
 </code> </code>
  
Line 48: Line 48:
 file.close() file.close()
 </code> </code>
 +
 +===== Run two commands in a pipe =====
 +
 +<code python>
 +cmd1 = ["oggdec", "-Q", "-o", "-", src]
 +cmd2 = ["lame", "--preset", "cd", "-", dst]
 +p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
 +p2 = subprocess.Popen(cmd2, stdin=p1.stdout, stdout=subprocess.PIPE)
 +p1.stdout.close()
 +output = p2.communicate()[0]
 +</code>
 +
 +===== Write to command standard input =====
 +
 +<code python>
 +cmd_input = []
 +cmd_input.append('line input one')
 +cmd_input.append('line input two')
 +
 +p = subprocess.Popen('command', stdin=subprocess.PIPE)
 +p.communicate(os.linesep.join(cmd_input))
 +</code>
 +
doc/appunti/prog/python_program_execution.txt · Last modified: 2022/07/01 10:56 by niccolo