python-the module that executes the shell command is used

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.