There are quite a lot of opportunities to use subprocess, so I will summarize the points I investigated without understanding.
This article is for Python 2.7
, as I assumed the Python 2.7
environment as my use case at the time of my research.
Basically, please refer to the official document below. 17.1. Subprocess — Subprocess Management — Python 2.7.x Documentation
Especially in the case of Python 3.5 or higher, the usage is different because the general-purpose run ()
command is added.
Please refer to the formula without diversion as it is.
17.5. Subprocess — Subprocess Management — Python 3.5.2 Documentation
import
import subprocess
subprocess.call -Runtime standard output / standard error is output at the time of execution ・ Returns a return code in both normal and abnormal conditions
Execution sample
import subprocess
print '!!! start'
cmd = 'echo aaa'
retcode = subprocess.call(cmd.split())
print retcode
cmd = 'false'
retcode = subprocess.call(cmd.split())
print retcode
print '!!! end'
output
aaa
!!! start
0
1
!!! end
subprocess.check_call
-Runtime standard output / standard error is output at the time of execution
・ Returns 0 return code when normal
-In case of abnormality, subprocess.CalledProcessError
exception is sent.
Execution sample
import subprocess
print '!!! start'
cmd = 'echo aaa'
retcode = subprocess.check_call(cmd.split())
print retcode
cmd = 'false'
try:
retcode = subprocess.check_call(cmd.split())
except subprocess.CalledProcessError as e:
print e.returncode
print e.cmd
print e.output
print '!!! end'
output
aaa
!!! start
0
1
['false']
None
!!! end
subprocess.check_output
-Runtime standard output / standard error is output at the time of execution
・ Returns standard output when normal
-In case of abnormality, subprocess.CalledProcessError
exception is sent.
Execution sample
import subprocess
print '!!! start'
cmd = 'echo aaa'
d = subprocess.check_output(cmd.split())
print d,
cmd = 'unexist_command'
try:
d = subprocess.check_output(
cmd.split(),
stderr=subprocess.STDOUT,
shell=True)
print d,
except subprocess.CalledProcessError as e:
print e.returncode
print e.cmd
print e.output,
print '!!! end'
output
!!! start
aaa
127
['unexist_command']
/bin/sh: unexist_command: command not found
!!! end
subprocess.check_output
Specify stderr = subprocess.STDOUT
as shown below.
Example of use
d = subprocess.check_output(
cmd.split(),
stderr=subprocess.STDOUT,
shell=True)
cwd
optionSpecifying the execution directory
subprocess.call(cmd, cwd=workdir)
An execution example is shown below.
Execution sample
import subprocess
d1 = subprocess.check_output(['pwd']) #Initial position/tmp
d2 = subprocess.check_output(['pwd'], cwd='/tmp/hoge') #Confirmation with absolute path
d3 = subprocess.check_output(['pwd']) #Not affected by other movements
d4 = subprocess.check_output(['pwd'], cwd='./hoge') #Confirmation with relative path
print 'case 1:'
print d1,
print d2,
print 'case 2:'
print d3,
print d4,
Execution example
case 1:
/tmp
/tmp/hoge
case 2:
/tmp
/tmp/hoge
python subprocess changing directory - Stack Overflow
When fetching an execution command from the outside, especially when shell = True
is specified, a command injection attack may occur, so be careful about security.
IPA ISEC Secure Programming Course: Web Application Chapter 6 Input Countermeasures: Command Injection Attack Countermeasures Code injection - Wikipedia
-When shell = True
is specified in check_output
, standard output / standard error may not be obtained. (Empty string is returned)
Use communicate () when receiving output in Python subprocess-Qiita To execute a command in python-with a mug!
Recommended Posts