Summary of common methods of python sys module

The last article summarizes the os module. This module and the sys module seem to be similar in name, but in fact they have little relationship. The os module is mainly responsible for interacting with the operating system, and these two modules are often used in combination, which can achieve a lot demand. The sys module is mainly responsible for interacting with the Python interpreter and provides a series of functions and variables for controlling the Python running environment. This article summarizes the usage of commonly used sys modules.

1 Returns the name of the operating system platform

sys.platform 
'win32'

2 Get the version information of the Python interpreter

sys.version
'3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]'

3 Return to the current default character encoding format

sys.getdefaultencoding()
'utf-8'

4 sys.argv When running this script, it is used to pass parameters to the interpreter. It is a list, the first parameter is the path of the script itself by default, and the content following the running py file is passed to the program as subsequent parameters.
Read the chestnut below; create sys.py as follows

import sys  
sys.hexversion
if len(sys.argv) == 1: 
    print (sys.argv[0])
elif sys.argv[1]=='version' : 
    print(sys.version)
elif sys.argv[1]=='platform':
    print(sys.platform)  
else : print('Vague instructions')

Call sys.py in cmd

C:\Users\yuanwanli> python C:\Users\sys.py
 C:\Users\sys.py

C:\Users\yuanwanli>python C:\Users\sys.py  platform
 win32

C:\Users\yuanwanli>python C:\Users\sys.py version
 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]

C:\Users\yuanwanli>python C:\Users\sys.py  x
 Vague instructions

5 sys.stdin #(standard input)
The function of sys.stdin.readline() is equivalent to input( ) ,, the default input format is a string, and the difference lies in two points:

  1. You can directly fill in the prompt text in the input() brackets, such as input('Please input a number:' ), but sys.stdin.readline() cannot;
  2. sys.stdin.readline() will get the last newline character'\n' of each line of data, while input() ignores;
a = input('please input int:')
b = sys.stdin.readline()     
print(list(a), list(b)) 
# 都输入 1
['1'] ['1', '\n']

sys.stdin.readline().strip('\n') can delete the newline character'\n' to achieve the same effect as input()

a = input('please input int:')
b = sys.stdin.readline().strip('\n')     
print(list(a), list(b)) 
# 都输入 1
['1'] ['1']

sys.stdin.readline() input is in string format, int and float are required, which can be converted using map function

a = sys.stdin.readline().strip('\n')
a = list(map(int, a.split()))
print(a)
# 输入1 2 
[1, 2]

6 sys.stdout #(standard output)
When we call print(x) to print an object in Python, we actually call sys.stdout.write(x+'\n')
print to print what you need to the console , And then append a newline character, as follows:

sys.stdout.write('hello\n')
print('hello')
hello
hello 

This is the basic usage of the sys module. For a more comprehensive method, please refer to the official sys document https://docs.python.org/zh-cn/3/library/sys.html?highlight=sys#module-sys

Guess you like

Origin blog.csdn.net/weixin_43705953/article/details/109201014