Python third-party library series of ten--commands Library

Source: Internet
Author: User
Tags linux shell commands

we're talking about using the commands module to execute Linux shell commands, and when we write operations scripts in Python, we often need to execute the Linux shell commands, and the commands module in Python is dedicated to calling Linux Shell command, and return the status and results, here are the 3 main functions of the commands module:
1.commands.getoutput (' Shell command ')
2.commands.getstatus (' file ')
3.commands.getstatusoutput (' Shell command ')
Explain separately:
One, commands.getoutput (' Shell command ')
Execute shell command, return result (String type)

Import commandscommands.getoutput (' pwd ') #/users/admin/pycharmprojects/test

Second, commands.getstatus (' file ')

This function has been discarded by Python and is not recommended, it returns the result of the Ls-ld file (String) (It is too strange to return the result, no wonder it was discarded)
Import commandscommands.getstatus (' Admin.tar ') # '-rw-rw-r--1 Oracle 829440 Jan 10:36 Admin.tar '
Third, commands.getstatusoutput (' Shell command ')
Executes the shell command, returns a tuple of two elements tuple (status, result), status int, and result is of type string.
CMD is executed in {cmd;} 2>&1, so the returned result contains standard output and standard error. This is one of the most used functions.
Import commandscommands.getstatusoutput (' pwd ') # (0, '/users/admin/pycharmprojects/test ')


Python third-party library series of ten--commands Library

Related Article

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.