Python module-command and sys

Source: Internet
Author: User
Tags exit in stdin

1.commands Module

A module for supporting the shell in a Linux system environment


1) getoutput ()

Return value only returns the result (string type), no way to determine whether the execution of the result is normal


Example

Import commands

cmd = "Ls/data/temp"

RESULT1 = commands.getoutput (cmd)

Print (type(RESULT1)) # type is str

Print (RESULT1)


Results:

<type ' str ' >

2.py



2) Getstatusoutout ()

The returned result is a tuple tuple, the first value is the receive status code, the int type, 0 for normal, not 0 for the exception, and the second value as a string, the result of the shell command execution


Example

Import commands

cmd = "Ps-ef"

STATUS,RESULT2 = commands.getstatusoutput (cmd)

Print (type(status))

Print (status)

Print (type(RESULT2))

Print (RESULT2)


Results:

<type ' int ' >

0

<type ' str ' >

UID PID PPID C stime TTY time CMD

Root 1 0 0 Oct09? 00:00:14/USR/LIB/SYSTEMD/SYSTEMD--switched-root--system--deserialize 21

Root 2 0 0 Oct09? 00:00:00 [Kthreadd]



2.sys Module

1) Get program parameters through SYS module

sys.argv[0]: The first argument, the script itself

sys.argv[1]: The second argument, the first parameter passed in


Example

import sys

print ( "argv[0]={0}   Argv[1]={1}" .format (sys.argv[ 0 ],SYS.ARGV [ 1

Results:

argv[0]=c:/users/test/pycharmprojects/a/a1.python.py argv[1]=parameter1



2) Sys.stdin,sys. STDOUT,sys. StdErr

The stdin, stdout, and stderr variables contain stream objects that correspond to standard I/O streams. If you need more control over the output, and print does not meet your requirements, you can also replace them, redirect output and input to other devices, or process them in a non-standard way


Example 1:sys.stdout and Print

import sys

sys.stdout.write ( "Hello" + " \n "

Print ("Hello")


Results:

Hello

Hello


Example 2:Sys.stdin and Raw_input

Import SYS

name1 = Raw_input ("Input your Name:")

Print (name1)

Print ' stdin_your_name: ' ,         # command to stay on the same line

name2 = Sys.stdin.readline () [:-1] #-1 to discard the ' \ n ' in input stream

Print (name2)


Results:

Input your NAME:HUANGZP

Huangzp

Stdin_your_name:huangzp

Huangzp



Example 3: Console redirection file

Import SYS

F_hander = Open ("Out.log","W")

Sys.stdout = F_hander

Print ("Hello")


Results:

Generates a Out.log file locally with the content Hello



3) Capture sys.exit (n) called

Executes to the end of the main program, the interpreter automatically exits, but if you want to exit the program halfway, you can call the Sys.exit function, with an optional integer parameter returned to the program that called it, indicating that you can capture the call to Sys.exit in the main program. (0 is normal exit, others are abnormal)


Example

Import SYS

def Exitfunc ():

print "Hello World"

Sys.exitfunc = Exitfunc # Set the function called when capturing

Print "Start"

sys.exit ( Span style= "Font-size:12px;font-family:simsun, Stsong;color:rgb (0,0,255); Background-color:rgb (255,255,255);" >1 )               # Exit Automatic call e xitfunc (), the program exits

Print "End" # does not execute print


Results:

Start

Hello World


Description

The Sys.exitfunc function is set and the Exitfunc function is called when the Sys.exit (1) is executed, and the contents of Sys.exit (1) are not executed because the program has exited



Python module-command and sys

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.