Python SYS Module
The SYS module provides a number of functions and variables to handle different parts of the Python runtime environment.
Handling command-line arguments
SYS.ARGV #现从程序外部向程序传递参数 [*],0 represents the program itself
#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2017/11/29 0029 11:31#@Author: MingImportSYSPrint(SYS.ARGV)ifLen (SYS.ARGV) > 1: Print(sys.argv[0])Print(sys.argv[1]) Print(sys.argv[2])Else: Print("no parameters passed")
c:\users\administrator\pycharmproject\myproject\myfunc>python ceshi.py arg1 arg2[' ceshi.py'arg1'arg2'] # Description SYS.ARGV is a list containing parameters ceshi.py #sys. argv[0] Represents the script itself arg1 # first parameter arg2 # first argument
Sys.path #获取指定模块搜索路径的字符串集合, you can put the well-written module in a given path, you can import in the program correctly found
# !/usr/bin/env python # -*-coding:utf-8-*- # @Time: 2017/11/29 0029 11:31 # @Author: Ming import sys print (type (Sys.path)) for i in Sys.path: print (i)
Run Result:<class'list'> # sys.path is a list The list contains the paths or folders that all the py files can be searched for, and only the modules or py files in that directory can be Imporp imported C:\Users\Administrator\PycharmProject\MyProject\myfuncC : \users\administrator\pycharmproject\myprojectc:\users\administrator\appdata\local\programs\python\python35\ Python35.zipc:\users\administrator\appdata\local\programs\python\python35\dllsc:\users\administrator\appdata\ Local\programs\python\python35\libc:\users\administrator\appdata\local\programs\python\python35c:\users\ Administrator\appdata\local\programs\python\python35\lib\site-packages
Sys.version #打印python版本
Sys.platform # Viewing the system version
Sys.getdefaultencoding () # Get system current encoding
sys.getfilesystemencoding () # Gets the file system using encoding, returns ' MBCS ' under Windows, and returns ' Utf-8 '
under Mac
# !/usr/bin/env python # -*-coding:utf-8-*- Import sys print (sys.version) Span style= "COLOR: #008000" ># View platform version print (sys.platform) # View system version print (sys.getdefaultencoding ()) # get system current encoding print (sys.getfilesystemencoding ()) # Get the file system using encoding, return ' MBCS ' under Windows, return ' Utf-8 ' under Mac
operation Result:3.5.1 (v3.5.1:37a07cee5969, Dec 6, 01:54:25) [MSC v.1900 (AMD64)]win32utf-8 MBCS
Sys.stdin #标准IO流, if you need more control over the output, and print does not meet your requirements, they are what you need. You can also replace them, so you can redirect output and input to other devices, or handle them in a non-standard way
Sys.stdout
Sys.stderr
making progress bar: Because standard input does not automatically enter # !/usr/bin/env python # -*- Coding:utf-8-*- import sys, TIME Span style= "COLOR: #0000ff" >for i in Range (51 " \r " ) Sys.stdout.write ( Span style= "COLOR: #800000" > " %s%% |%s " % (int (I/50 *), I * | " 0.05)
running results: Dynamic display of the following 100% | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Python SYS Module