10 of the python third-party Library Series-commands library, python-commands

Source: Internet
Author: User
Tags linux shell commands

10 of the python third-party Library Series-commands library, python-commands
We are talking about using the commands module to execute Linux shell commands. When we use Python to write O & M scripts, we often need to execute linux shell commands, the commands module in Python is used to call the Linux shell Command and return the status and result. The following are the three main functions of the commands module:
1. commands. getoutput ('Shell command ')
2. commands. getstatus ('file ')
3. commands. getstatusoutput ('Shell command ')
Explanations:
1. commands. getoutput ('Shell command ')
Run the shell command to return the result (string type)

import commandscommands.getoutput('pwd')#/Users/admin/PycharmProjects/test

2. commands. getstatus ('file ')

This function has been discarded by python and is not recommended. It returns the ls-ld file Result (String) (the returned result is so strange that it cannot be discarded)
import commandscommands.getstatus('admin.tar')#'-rw-rw-r-- 1 oracle oracle 829440 Jan 29 10:36 admin.tar'
3. commands. getstatusoutput ('Shell command ')
Run the shell command to return the tuple (status, result) of the two elements. status indicates int type, and result indicates string type.
The cmd execution method is {cmd;} 2> & 1. Therefore, the returned results include standard output and standard errors. This is the most commonly used function.
import commandscommands.getstatusoutput('pwd')#(0, '/Users/admin/PycharmProjects/test')


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.