doc:appunti:prog:python_program_execution
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| doc:appunti:prog:python_program_execution [2011/05/13 10:18] – [Get the output of a program] niccolo | doc:appunti:prog:python_program_execution [2022/07/01 10:56] (current) – [Get the output of a program] niccolo | ||
|---|---|---|---|
| Line 17: | Line 17: | ||
| </ | </ | ||
| - | Same as above, but run in a subshell: | + | Same as above, but run in a subshell |
| <code python> | <code python> | ||
| - | retcode = call(" | + | retcode = subprocess.call(" |
| </ | </ | ||
| Line 28: | Line 28: | ||
| <code python> | <code python> | ||
| - | output = subprocess.Popen([" | + | output = subprocess.Popen([" |
| </ | </ | ||
| 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(' |
| - | print line | + | print(line) |
| </ | </ | ||
| Line 48: | Line 48: | ||
| file.close() | file.close() | ||
| </ | </ | ||
| + | |||
| + | ===== Run two commands in a pipe ===== | ||
| + | |||
| + | <code python> | ||
| + | cmd1 = [" | ||
| + | cmd2 = [" | ||
| + | p1 = subprocess.Popen(cmd1, | ||
| + | p2 = subprocess.Popen(cmd2, | ||
| + | p1.stdout.close() | ||
| + | output = p2.communicate()[0] | ||
| + | </ | ||
| + | |||
| + | ===== Write to command standard input ===== | ||
| + | |||
| + | <code python> | ||
| + | cmd_input = [] | ||
| + | cmd_input.append(' | ||
| + | cmd_input.append(' | ||
| + | |||
| + | p = subprocess.Popen(' | ||
| + | p.communicate(os.linesep.join(cmd_input)) | ||
| + | </ | ||
| + | |||
doc/appunti/prog/python_program_execution.1305274690.txt.gz · Last modified: by niccolo
