When you want to hit a UNIX command on Python

Overview

For those who want to batch execute Mac terminal commands in Python

environment

· Mac OS Sierra 10.12.1 ・ Python 3.5.2

manner

import subprocess
subprocess.call(["command"])

only this

Example of use

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.

Error: When a Segmentation fault appears

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.

reference

-Subprocess management -How to execute a command using subprocess in Python

Recommended Posts

When you want to hit a UNIX command on Python
When you want to play a game via Proxy
When you want to plt.save in a for statement
Solution when you want to use cv_bridge with python3 (virtualenv)
Hit a command in Python (Windows)
When you want to use python2.x on modern Gentoo Linux
[Subprocess] When you want to execute another Python program in Python code
A note on what you did to use Flycheck with Python
I want to know if you install Python on Mac ・ Iroha
When you want to sort a multidimensional list by multiple lines
When you want a long line break
When you want to replace multiple characters in a string without using regular expressions in python3 series
Things to watch out for when creating a Python environment on a Mac
Gist repository to use when you want to try a little with ansible
When you want to replace a column with a missing value (NaN) column by column
Python Note: When you want to know the attributes of an object
Wrapper when you want to output utf-8 + ansi color on Windows console
If you want to create a Word Cloud.
When you want to update the chrome driver.
Python Note: When assigning a value to a string
How to remember when you forget a word
Try to create a new command on linux
I want to write to a file with Python
How to write environment variables that you don't want to put on [GitHub] Python
If you want to make a TODO application (distributed) now using only Python
How to manage arguments when implementing a Python script as a command line tool
[Python] What to check when you get a Unicode Decode Error in Django
If you want to make a discord bot with python, let's use a framework
What to do when you want to receive files from a Windows client remotely
I want to be notified when the command operation is completed on linux!
[Python3] Code that can be used when you want to cut out an image in a specific size
How to deal with "^ [[A ^ [[B ^ [[C ^ [[D"] when you press the arrow keys when executing python on mac
[Grasshopper] When creating a data tree on Python script
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want DQN Puniki to hit a home run
I want to generate a UUID quickly (memorandum) ~ Python ~
Try to make a command standby tool with python
I want to write in Python! (2) Let's write a test
Settings when you want to run python-mecab with travis
Yum command to access MySQL with Python 3 on Linux
I want to find a popular package on PyPi
I want to randomly sample a file in Python
How to build a Django (python) environment on docker
When you want to filter with Django REST framework
I want to work with a robot in python.
Things to note when initializing a list in Python
[Python] I want to make a nested list a tuple
I want to AWS Lambda with Python on Mac!
How to execute a command using subprocess in Python
How to build a Python environment on amazon linux 2
I want to run a quantum computer with Python
I want to do something in Python when I finish
What to do when you can't bind CaboCha to Python
A story that got stuck when trying to upgrade the Python version on GCE
[Django] Hit a command you made from within the process that runs on manage.py.
How to pass arguments when invoking python script from blender on the command line
[Linux] When you want to search for a specific character string from multiple files
A little trick to know when writing a Twilio application using Python on AWS Lambda
[Python] If you suddenly want to create an inquiry form