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.
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