How to execute a command using subprocess in Python

Overview

I will show you how to execute a command using the subprocess module in Python.

Why subprocess

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.

Method to execute command

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.

Sample code

import subprocess

subprocess.call( ["ls", "-l"] ) 

Example of use

I used it when executing a jar file from a Python script.

Reference site

"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

How to execute a command using subprocess in Python
How to get a string from a command line argument in python
How to receive command line arguments in Python
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
How to implement Discord Slash Command in Python
How to create a JSON file in Python
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to generate a new loggroup in CloudWatch using python within Lambda
[Python] Execute unix command (subprocess)
How to develop in Python
[For beginners] How to use say command in python!
How to convert / restore a string with [] in python
How to set up a Python environment using pyenv
[Python] How to expand variables in a character string
How to make a Python package using VS Code
How to exit when using Python in Terminal (Mac)
How to retrieve multiple arrays using slice in python.
How to pass the execution result of a shell command in a list in Python
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to install python using anaconda
How to write a Python class
[Python] How to do PCA in Python
Hit a command in Python (Windows)
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to run a Python file at a Windows 10 command prompt
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
How to transpose a 2D array using only python [Note]
I tried "How to get a method decorated in Python"
How to generate a query using the IN operator in Django
To return char * in a callback function using ctypes in Python
How to get a list of built-in exceptions in python
I tried to make a stopwatch using tkinter in python
How to execute external shell scripts and commands in python
How to unit test a function containing the current time using freezegun in python
How to host web app backend processing in Python using a rental server subdomain
[Introduction to Python] How to use class in Python?
[sh] How to store the command execution result in a variable
How to determine the existence of a selenium element in Python
How to access environment variables in Python
How to pass arguments to a Python script in SPSS Modeler Batch
How to dynamically define variables in Python
[Python] How to make a class iterable
How to do R chartr () in Python
How to make a string into an array or an array into a string in Python
How to draw a graph using Matplotlib
[Introduction to Python] How to output a character string in a Print statement
[Itertools.permutations] How to put permutations in Python
How to check the memory size of a variable in Python
How to create an instance of a particular class from dict using __new__ () in python