Recently, learning other people's blog, summed up the next few knowledge points, in case of a rainy days, interview occasionally asked: a result of a variety of methods. The following are:
Gets the return value of the system execution command and the output result:
1. Os.system: Call system command, exit after completion, display output, return value (General execution success is 0).
>>> import OS
>>> os.system (' pwd ')
/home/ubuntu
0
2, Os.popen: Returns the file read object, and then read () The operation can see the output of the execution
>>> Import OS
>>> a=os.popen (' pwd ')
>>> Print a
<open file ' pwd ', mode ' R ' at 0x29c2c00>
>>> Print A.read ()
/home/ubuntu
3. Commands.getstatusoutput: Get the return value and output
>>> Import Commands
>>> (status, Output) = Commands.getstatusoutput (' pwd ')
>>> Print Status,output
0/home/ubuntu
>>> commands.getstatusoutput (' pwd ')
(0, '/home/ubuntu ')
This article is from the "Snail's Home" blog, be sure to keep this source http://winters.blog.51cto.com/5617866/1590712
python-the module that executes the shell command is used