Python methods for executing system commands Os.system (), Os.popen (), commands

Source: Internet
Author: User

Reprint: http://blog.csdn.net/b_h_l/article/details/12654749

The first type: Using Os.system ()

Import OS
Os.system ('cat/etc/profile')

The second type: Using Os.popen ()

= Os.popen ('cat/proc/cpuinfo') print output.read ()

The object that is returned by Os.popen () is a file read that reads the read () operation to see the output that is executed

The third type: using commands

Import Commands (status,output)=commands.getstatusoutput ('Cat/etc/profile') Print Statusprint output#===============output in Linux=commands.getoutput ('Cat/etc/profile') Status=commands.getstatus ('Cat/etc/profile')#===============You can also use the window (status,output)=commands.getstatusoutput ('dir') Print Statusprint output

Summary: The first is the most common, but sometimes need to master the results of command execution, at this time we can use the third

Python methods for executing system commands Os.system (), Os.popen (), commands

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.