Get the return value of an external shell script (ls) with python3

1. What you want to do

I want to get the return value by hitting ls or pwd from Python3. I always forget it, so make a note The other articles are a little old so I feel new If it is ls, glob.glob I hear that it can be used, but this time it is in the through direction.

2. How to write (command-> subprocess)

command.getoutput(cmd)1 But ** subprocess **. getoutput (cmd) 2 </ sup>

Get the return value of the shell!



#Command is abolished in 3 series, so use subprocess instead
from subprocess import getoutput  

ls = getoutput('ls')
print(ls)
 #Return value of ls (str)

pwd = getoutput('pwd')
print(pwd)
 #Absolute path of the current directory (str)


top = getoutput('top') #Will not come back forever

that's all


1. http://docs.python.jp/2/library/commands.html#commands.getoutput

2. http://docs.python.jp/3.5/library/subprocess.html#subprocess.getoutput

Recommended Posts

Get the return value of an external shell script (ls) with python3
Get the return code of the Python script from bat
[Python] Get the script execution directory with an absolute path
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
Get the value of a specific key in a list from the dictionary type in the list with Python
Get the source of the page to load infinitely with python.
Get the weather with Python requests
Get the weather with Python requests 2
About the return value of pthread_mutex_init ()
Get the script path in Python
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
Get the attributes of an object
About the return value of the histogram.
I tried to create a Python script to get the value of a cell in Microsoft Excel
Process the contents of the file in order with a shell script
Get and set the value of the dropdown menu using Python and Selenium
Tips: [Python] Calculate the average value of the specified area with bedgraph
PhytoMine-I tried to get the genetic information of plants with Python
Execute Python script with cron of TS-220
The story of making Python an exe
Check the existence of the file with python
[Python] Get the variable name with str
Get the value of the middle layer of NN
[Python] Get the character code of the file
Get the width of the div on the server side with Selenium + PhantomJS + Python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
I tried to get the authentication code of Qiita API with Python.
Get the number of articles accessed and likes with Qiita API + Python
I tried to get the movie information of TMDb API with Python
Get and estimate the shape of the head using Dlib and OpenCV with python
[Python] Get the files in a folder with Python
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Python] Get / edit the scale label of the figure
Find the mood value with python (Rike Koi)
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
Calculate the total number of combinations with python
Watch out for the return value of __len__
Check the date of the flag duty with Python
[Python] Get rid of dating with regular expressions
Compare the sum of each element in two lists with the specified value in Python
Python script to get a list of input examples for the AtCoder contest
Note: How to get the last day of the month with python (added the first day of the month)
Convert the character code of the file with Python3
Examine Python script bottlenecks with the cProfile module
[Python] Get the day of the week (English & Japanese)
Get the stock price of a Japanese company with Python and make a graph
[Python] Determine the type of iris with SVM
Python script to get note information with REAPER
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
Get the update date of the Python memo file.
Summary of reference sites when editing Blender Script with an external editor (VS Code)
Get an Access Token for your service account with the Firebase Admin Python SDK
Get information equivalent to the Network tab of Chrome developer tools with Python + Selenium
An easy way to pad the number with zeros depending on the number of digits [Python]
[Python & SQLite] I analyzed the expected value of a race with horses with a win of 1x ②
Extract the table of image files with OneDrive & Python
Template of python script to read the contents of the file
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)