I will show you how to execute a command using the subprocess module in Python.
As Python versions went up, the old commands module was no longer used, and it was officially recommended to use the Subprocess module instead of the os module.
So, this time, I wrote it because there were not many simple articles on Qiita about how to execute commands in Python using the Subprocess module.
There are several methods in the subprocess that execute the command. Let's look at that method first.
call(cmd) Executes the command of the argument and returns 0 if the execution is successful.
check_call(cmd) In addition to the call-like behavior, it throws an exception called CalledProcessError if execution fails.
check_output(cmd) Returns the output of the command execution result as a byte string. This also throws an exception called CalledProcessError if execution fails.
import subprocess
subprocess.call( ["ls", "-l"] )
I used it when executing a jar file from a Python script.
"17.1. Subprocess — Subprocess Management" http://docs.python.jp/2/library/subprocess.html "17.5. Subprocess — Subprocess Management" http://docs.python.jp/3/library/subprocess.html "To execute commands with Python" http://takuya-1st.hatenablog.jp/entry/2014/08/23/022031 "Subprocess module" http://www-he.scphys.kyoto-u.ac.jp/member/shotakaha/dokuwiki/doku.php?id=toolbox:python:subprocess 「Learning 1 documentation SUBPROCESS」http://her0.web.fc2.com/python/standard/subprocess.html
Recommended Posts