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
doc:appunti:prog:python_program_execution [2016/07/28 09:42] niccolodoc:appunti:prog:python_program_execution [2022/07/01 10:56] (current) – [Get the output of a program] 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 28: Line 28:
  
 <code python> <code python>
-output = subprocess.Popen(["mycmd", "myarg"], stdout=subprocess.PIPE).communicate()[0]+output = subprocess.Popen(["mycmd", "myarg"], stdout=subprocess.PIPE).communicate()[0].decode('utf-8')
 </code> </code>
  
Line 37: Line 37:
 output, stderr = subproc.communicate() output, stderr = subproc.communicate()
 retcode = subproc.returncode retcode = subproc.returncode
-for line in output.splitlines(): +for line in output.decode('utf-8').splitlines(): 
-    print line+    print(line)
 </code> </code>
  
Line 59: Line 59:
 output = p2.communicate()[0] output = p2.communicate()[0]
 </code> </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.1469691754.txt.gz · Last modified: 2016/07/28 09:42 by niccolo