External command execution in Python

@ Shared server / Environment without pyenv or ʻanaconda`

ʻGoogling what corresponds to IPC :: Open3`. ↓

subprocess is recommended.

However,,,

In the Python 2 series, the standard is not used, and it is clearly stated that subprocess32 can be used.

What's that? I wish I could replace it normally.

I don't have root privileges, so use pip to enter it locally.

Installation of subprocess32

Of course, the version of Python should be 2 series.

python


$ pip install --install-option="--prefix=$HOME/share" subprocess32
#$ export PYTHONPATH=$HOME/share/lib64/python2.6/site-packages:

So change the library call between 2 and 3

python


import sys
import platform
py_version = platform.python_version_tuple()[0]

if  py_version == 2:
    sys.path.append('/home/bunzaemon/share/lib64/python2.6/site-packages')
    from subprocess32 import Popen, PIPE
else:
    from subprocess   import Popen, PIPE

I think it's more like Python to use try, but ...

Popen

Popen

--String type in 2 series --Byte type in 3 series

return it. It cannot be handled properly unless it is changed to string [^ 2].

[^ 2]: The script doesn't pass unless decode ("utf-8 ") is output even though ʻutf-8is not output @Python2.6.6`

python


cmd  = 'ls -al'
p = Popen(cmd.split(), stdout=PIPE, stderr=PIPE)
out = ( p.communicate()[0].split("\n")            if py_version == 2 else
        p.communicate()[0].decode("utf-8").split("\n") )

for line in out:
    print (line)

Error in comunicate () [1].

You can use it like ʻIPC :: Open3normally, but for the time being, let's get used to the recommendedcomunicate`.

All sources

python


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Importing libraries
import sys
import platform

py_version = platform.python_version_tuple()[0]
if  py_version == 2:
    sys.path.append('/home/bunzaemon/share/lib64/python2.6/site-packages')
    from subprocess32 import Popen, PIPE
else:
    from subprocess   import Popen, PIPE

cmd  = 'ls -al'
p = Popen(cmd.split(), stdout=PIPE, stderr=PIPE)
out = ( p.communicate()[0].split("\n")            if py_version == 2 else
        p.communicate()[0].decode("utf-8").split("\n") )

for line in out:
    print (line)

Recommended Posts

External command execution in Python
Execute external command in python
Existence check of external command in Python (like `which`)
Check for external commands in python
Run shell command / python in R
Measure function execution time in Python
In the python command python points to python3.8
Python unittest module execution in vs2017
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Debug step execution in Python (Bottle, Intellij)
Unittest in python
[LLDB] Create your own command in Python
Learn the design pattern "Command" in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
StepAIC in Python
Decompose command arguments in one line in Python
N-gram in python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Parallel task execution using concurrent.futures in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Template for creating command line applications in Python
How to receive command line arguments in Python
How to implement Discord Slash Command in Python
Get keystrokes during background execution in Python (windows)
Load and execute command from yml in python
Command execution triggered by file update (python edition)
Daily AtCoder # 36 in Python
Clustering text in Python
Implement Enigma in python