Execute shell process with subprocess module (Python 3.5)
goal and general advices
goal:
The subprocess module allows you to spawn new processes, connect to their input/output/error
pipes, and obtain their return codes.
This module intends to replace several older modules and functions:
os.system
os.spawn*
Recommended approach: use the run()
function for all use cases it can
handle. For more advanced use cases, the underlying Popen
interface can be used
directly.
main functions
run()
Run the command described by args. Wait for command to complete, then return a CompletedProcess
instance.
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)
Popen()
Execute a child program in a new process.
main use cases
Notes: Prior to Python 3.5, these three functions : call()
, check_call()
, check_output()
comprised the high level API to subprocess. You can now use
run()
in many cases.
Main use cases:
– Run a command, wait for its completion and return the exit code:
run(...).returncode
(before we used subprocess.call()
)
Example:
# for windows command we need use the shell to True
return_code: int = subprocess.run(["ddir", "/S"], shell=True).returncode
print(f'return_code={return_code}')
return_code: int = subprocess.run(["dir", "/S"], shell=True).returncode
# return_code: int = subprocess.run(["ls", "-l"]).returncode
print(f'return_code={return_code}') |
-Run a command, wait for its completion, expecting a success and capture stdout or stderr:
run(..., check=True)
(before we used subprocess.check_output()
)
Example:
subprocess.run(["dir", "/S"], shell=True,
check=True)
print('we see that message')
subprocess.run(["ddir", "/S"], shell=True, check=True)
print("we don't see that message") |
-Run a command, wait for its completion,expecting a success and return its output.
run(..., check=True, stdout=PIPE).stdout
(before we used subprocess.check_output()
)
Example:
completed_process: subprocess.CompletedProcess = subprocess.run(["dir", "/S"], shell=True,
check=True,
stdout=subprocess.PIPE)
print(f'output={completed_process.stdout}')
completed_process: subprocess.CompletedProcess = subprocess.run(["ddir", "/S"], shell=True,
check=True,
stdout=subprocess.PIPE)
print('we never reach this statement') |
-run a command and don’t wait for its completion,instead run the command as a child process:
Example:
# creationflags is optional
subprocess.Popen(["C:\\Program Files\\Talon\\python.exe", "C:\\Program Files\\Talon\\repl.py"],
creationflags=subprocess.CREATE_NEW_CONSOLE) |
Imports
Example with the old API
subprocess.call('ls -lrt /tmp', shell=True) |
Commands span on several line with EOF
cmd_sql_plus_oracle: str = (
"sqlplus -s -l / as sysdba >> EOF"
"nCREATE OR REPLACE DIRECTORY fooDir AS '/apps/oracle/fooDir';"
"nGRANT READ, WRITE ON DIRECTORY fooDir TO foo_user;"
"nexit; "
"nEOF"
)
subprocess.call(cmd_sql_plus_oracle, shell=True) |
Ce contenu a été publié dans
Non classé. Vous pouvez le mettre en favoris avec
ce permalien.