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 revisionNext revisionBoth sides next revision | ||
doc:appunti:prog:python_program_execution [2009/10/01 15:37] – niccolo | doc:appunti:prog:python_program_execution [2017/01/13 12:14] – [Run a command] niccolo | ||
---|---|---|---|
Line 14: | Line 14: | ||
<code python> | <code python> | ||
- | retcode = subprocess.call([" | + | retcode = subprocess.call([" |
+ | </ | ||
+ | |||
+ | Same as above, but run in a subshell (in this case the command may contain shell redirections): | ||
+ | |||
+ | <code python> | ||
+ | retcode = subprocess.call(" | ||
</ | </ | ||
Line 25: | Line 31: | ||
</ | </ | ||
- | Get also the return code and iterate on the output: | + | Get also the return code, stderr |
<code python> | <code python> | ||
- | subproc = subprocess.Popen([" | + | subproc = subprocess.Popen([" |
- | output | + | output, stderr |
retcode = subproc.returncode | retcode = subproc.returncode | ||
for line in output.splitlines(): | for line in output.splitlines(): | ||
Line 35: | Line 41: | ||
</ | </ | ||
+ | ===== Redirect output to a file ===== | ||
+ | |||
+ | <code python> | ||
+ | file = open("/ | ||
+ | subprocess.call([" | ||
+ | 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] | ||
+ | </ |
doc/appunti/prog/python_program_execution.txt · Last modified: 2022/07/01 10:56 by niccolo