How to handle Linux commands well from Python

There are many ways to handle Linux commands from Python, but the best way is to use the Popen class, which can handle standard output and standard error output structurally.

Functions that appear

Function name Classification Mold
Popen().stdout.readline() Standard output Only one line is output from the result str
Popen().stdout.readlines() Standard output Output all results as an array list[str]
Popen().stderr.readline() Standard error output Only one line is output from the result str
Popen().stderr.readlines() Standard error output Output all results as an array list[str]

Handle standard output and standard error output from Python

output.py


# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from subprocess import PIPE, Popen


def cmdline(command):
    """
Execute a command. shell=If True, execute via the shell.
    :param command: str
    :return: Popen
    """
    return Popen(
        args=command,
        stdout=PIPE,
        stderr=PIPE,
        shell=True
    )


#Standard output
print 'Standard output:' + cmdline('date').stdout.readline()

#Standard error output
print 'Standard error output: ' + cmdline('echo 2015|xargs wc -l').stderr.readline()

Execution result


>>> python output.py
Standard output:Monday, December 7, 2015 18:47:09 JST
Standard error output: wc: 2015: open: No such file or directory

Get the latest commit hash of git

git.py


#print the latest commit hash of git
print cmdline('git log --oneline').stdout.readline()

#Print the latest commit hash from another repository
print cmdline('cd ~/pypi/cf_recommender/ && git log --oneline').stdout.readline()

Execution result


>>> python git.py
6a2ae0a xlsx update by automatic
a2febc4 update readme

Error notification in chat when standard error exists

chat.py


# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from subprocess import PIPE, Popen


def cmdline(command):
    """
Execute a command
    :param command: str
    :return: Popen
    """
    return Popen(
        args=command,
        stdout=PIPE,
        stderr=PIPE,
        shell=True
    )


def chat(message):
    #Dummy implementation
    print 'ERROR:' + message


#Notify the error content by chat when standard error is output
cmd = [
    'python manage.py test_json',
    'python manage.py test_battle',
]

#Command execution prefix, &&Chain multiple commands with
prefix = 'cd ~/application/ && {}'

for _cmd in cmd:
    command = prefix.format(_cmd)
    p = cmdline(command)
    print 'command:' + command

    #Chat to notify when standard error is present
    errors = p.stderr.readlines()
    if errors:
        error_message = ''.join(errors)
        chat(error_message)

Execution result


command:cd ~/application/ && python manage.py test_json
ERROR:Traceback (most recent call last):
  File "manage.py", line 10, in <module>
  ...
IOError: [Errno 2] No such file or directory: u'..../test.json'

command:cd ~/application/ && python manage.py test_battle

I'm sure there are people who know more convenient ways (・ ㅂ ・) و

Recommended Posts

How to handle Linux commands well from Python
How to access wikipedia from python
How to handle Japanese in Python
How to update Google Sheets from Python
How to access RDS from Lambda (python)
How to operate Linux from the console
[Amazon Linux] Switching from Python 2 series to Python 3 series
How to open a web browser from python
How to install Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[Python] How to read data from CIFAR-10 and CIFAR-100
Linux commands to remember
How to generate a Python object from JSON
[Introduction to Python] How to handle JSON format data
How to operate Linux from the outside Procedure
[Python] How to handle Japanese characters with openCV
How to handle datetime type in python sqlite3
[Python] How to remove duplicate values from the list
How to "cache enabled" access to FPGA memory from Linux
How to scrape image data from flickr with python
Beginners! Basic Linux commands and how to use them!
How to download files from Selenium in Python in Chrome
Execute Python function from Powershell (how to pass arguments)
How to build a Python environment on amazon linux 2
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
How to handle JSON in Ruby, Python, JavaScript, PHP
ODBC access to SQL Server from Linux with Python
Post from Python to Slack
How to install Python [Windows]
python3: How to use bottle (2)
Cheating from PHP to Python
How to update Python Tkinter to 8.6
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
Switch from python2.7 to python3.6 (centos7)
How to run Notepad ++ Python
Connect to sqlite from python
How to change Python version
How to develop in Python
How to handle data frames
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to use linux commands in Visual Studio Code terminal
How to slice a block multiple array from a multiple array in Python
How to run a Python program from within a shell script
How to launch AWS Batch from a python client app
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
How to sample from any probability density function in Python
How to execute external shell scripts and commands in python
Let's use Watson from Python! --How to use Developer Cloud Python SDK
How to call Python or Julia from Ruby (experimental implementation)
Call Matlab from Python to optimize