How to execute external shell scripts and commands in python

How to execute an external shell script or command in python.

This time, we assume the operation of executing "ip-address_check.sh" to check whether the IP address entered in the python code is correct, and storing and displaying the result.

When it comes to executing an external shell script, there are two patterns, but the return values are different.

  1. os.system('ls -la') If the command is successful, 0 will be returned.

  2. commands.getoutput("ls -la") The result of the actual execution will be returned.

#!/usr/bin/env python

import commands

check = commands.getoutput("./ip-address_check.sh 192.168.1.1")

print check

Since ip-address_check is created to return OK if there is no problem with the format, in this case print check will also output OK as the output result of python.

It's easy to forget the command, so make a note of it.


I've been in python for half a year, so I'll expose the reference books I read. http://komeiy.hatenablog.com/entry/2014/11/28/223223

Recommended Posts

How to execute external shell scripts and commands in python
How to package and distribute Python scripts
How to execute commands in jupyter notebook
How to use is and == in Python
How to generate permutations in Python and C ++
How to plot autocorrelation and partial autocorrelation in python
How to write the correct shebang in Perl, Python and Ruby scripts
Run shell commands in python
Execute external command in python
How to develop in Python
[Python] How to sort dict in list and instance in list
Execute external commands in python (both receiving/not receiving results)
How to execute a command using subprocess in Python
Check for external commands in python
How to collect images in Python
How to swap elements in an array in Python, and how to reverse an array.
How to use SQLite in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
[Blender] How to handle mouse and keyboard events in Blender scripts
How to handle Japanese in Python
How to log in to AtCoder with Python and submit automatically
How to install OpenCV on Cloud9 and run it in Python
How to use functions in separate files Perl and Python versions
Difference in how to write if statement between ruby ​​and python
[ROS2] How to describe remap and parameter in python format launch
How to display bytes in the same way in Java and Python
Using verticalenv in shell scripts
How to execute external shell scripts and commands in python
If FTP doesn't connect, check the user's login shell and / etc / shells
How to package and distribute Python scripts
Easily graph data in shell and Python
[Introduction to Python] How to use class in Python?
How to dynamically define variables in Python
How to install and use pandas_datareader [Python]
How to do R chartr () in Python
python3 How to install an external module
How to work with BigQuery in Python
[Ubuntu] How to execute a shell script
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
Easily graph data in shell and Python
python: How to use locals () and globals ()
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
How to use regular expressions in Python
How to display Hello world in python
How to write Ruby to_s in Python
How to automatically execute python script (with GUI) and sudo permission command after logging in with Ubuntu 18.04
How to get the date and time difference in seconds with python
How to put a half-width space before letters and numbers in Python.
How to stop a program in python until a specific date and time
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python