[Python] Execute unix command (subprocess)

Setting

environment: OSX Yosemite Bash Python3.5.0

I want to get the output of Unix commands on Python.

Unix commands

OS X on Mac is based on UNIX, so I will use it. (ls, touch, cat, etc.)

os.system Standard library ʻos` I can use Unix commands here as well, but I can't get the return value as a string. (Maybe you can get it, but I don't know) (It's just output to Terminal) Normally, if it is a file operation system of commands such as touch, this is enough. If it is a command such as cat or grep, it cannot be obtained as a character string.

subprocess Click here for the reference link 17.5. subprocess — Subprocess management Use subprocess.check_output in this. As a usage, you put a character string in the argument, but there are two ways, first, if you use it by default, check_output(["cut", "-d", "test.txt"]) will do. The other is to use shell = True as an argument, which can be executed by passing a string. check_output("cut -d test.txt", shell=True) For details, it is written in the URL that I referred to.

On Unix with shell = False (default): In this case, the Popen class uses os.execvp () to execute the child program. If a string is given as an argument, it will be used as the name or path of the program to be executed; however, the program will only work with no arguments.

If shell = True on Unix: If args is a string, specify a command line string to be executed through the shell. The string must exactly match the format you type at the shell prompt. For example, if you have a filename with spaces in the string, you need to quote or backslash escape. If args is a string, the first element is passed to the shell as a string representing the command name and the remaining elements as subsequent arguments. This is equivalent to Popen below.

Simple sample

This is a sample (cut -d, -f2) that creates a file appropriately and acquires the second column. Since it is a 3 system, the value will be returned in bytes. (There seems to be an option (keyword argument) that returns str) In the case of 3 series, bytes are returned, so it is at the end of the code, but if you add ʻuniversal_newlines = True`, it will be returned as str.

subprocess_test.py


>>> open("test.txt", 'w').write('\n'.join(([repr([i, i+1, i+2]).replace(' ', '') for i in range(5)])))
39
>>> open("test.txt").read()
'[0,1,2]\n[1,2,3]\n[2,3,4]\n[3,4,5]\n[4,5,6]'
>>>
>>> import subprocess
>>> subprocess.check_output(["cut", "-d,", "-f2", "test.txt"])
b'1\n2\n3\n4\n5\n'
>>> subprocess.check_output("cut -d, -f2 test.txt", shell=True)
b'1\n2\n3\n4\n5\n'
>>> subprocess.check_output("cut -d, -f2 test.txt", shell=True, universal_newlines=True)
'1\n2\n3\n4\n5\n'

Summary

Generally, it is faster to write ShellScript, but for reference, I wrote how to execute Unix commands in Python.

Recommended Posts

[Python] Execute unix command (subprocess)
Execute command from Python
Execute command from python
Execute external command in python
Run unix command on python
How to execute a command using subprocess in Python
Load and execute command from yml in python
Automatically execute python file
python subprocess wrapper class
Basic Python command memo
External command execution in Python
Python eval can execute statements.
When you want to hit a UNIX command on Python
[Subprocess] When you want to execute another Python program in Python code
Execute Python script from batch file
Command line argument processing (Python docopt)
Hit a command in Python (Windows)
exec, eval to execute [python] statement
[Personal memo] Python virtual environment command memo
In the python command python points to python3.8
Timestamp acquisition command in Unix environment
Have python read the command output
Execute Python code from C # GUI
Execute Python Script during CodeSys # RunTime
Easy parallel execution with python subprocess
Command for the current directory Python