For those who want to batch execute Mac terminal commands in Python
· Mac OS Sierra 10.12.1 ・ Python 3.5.2
import subprocess
subprocess.call(["command"])
only this
import subprocess
# hello.Create txt, the contents are empty
subprocess.call(["touch", "hello.txt"]) # --1
# hello.Write hello world to txt
subprocess.call(["echo hello world > hello.txt"], shell = True) # --2
There are two ways to write it. In the case of 1 above, the blank space in UNIX is separated by ",".
The second is to give a character string to the first argument and set shell = True
.
The default is shell = False
, and it is not recommended because there is a description that setting it to True is a security threat even in the official.
However, at the moment, if you try to execute a command like 2 with the writing style of 1, you will only see the characters on the terminal, so you do not know how to solve it.
When I first tried to run it, I got the error message Segmentation fault
.
I didn't understand even if I googled it, and when I tried it with python3, it worked. If you have python3 installed, you can execute it by typing python3 index.py
, so if you have the same phenomenon, please execute it.
-Subprocess management -How to execute a command using subprocess in Python
Recommended Posts