SYS module has many functions, the following describes a few common modules.
1.SYS.ARGV: Passing parameters from outside to inside the program
# !/usr/bin/env python Import SYS Print (Sys.argv[0]) # Argv[0] Indicates the name of the function Print (Sys.argv[1]) # first parameter Print (Sys.argv[2]) # second parameter ~
Execution Result:
[[Email protected] script] # python sys_argv.py frist_argv second_argv SYS_ARGV.PYFRIST_ARGVSECOND_ARGV
2.sys.exit (n):
Execution to the end of the main program, the interpreter automatically exits, but if you need to exit 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, the rest is an exception.) )
3.sys.path ():
You can get a collection of strings for the specified module search path, and you can place the well-written module under a path that can be found when the program is import.
Import sys>>> sys.path['/usr/local/python3.6/lib/python36.zip ' ' /usr/local/python3.6/lib/python3.6 ' ' /usr/local/python3.6/lib/python3.6/lib-dynload ' ' /usr/local/python3.6/lib/python3.6/site-packages ']
4.sys.modules ():
Sys.modules () is a global dictionary that is loaded in memory after the python is started. Whenever a programmer imports a new module, Sys.modules will automatically record the module. When the module is imported again the second time, Python looks directly from the dictionary to speed up the program. It has all the methods that the dictionary has.
# !/usr/bin/env python Import SYS Print (Sys.modules.keys ()) Print (Sys.modules.values ()) Print (sys.modules['os')
5. sys.platform():
get the current system platform
>>> sys.platform'linux'
6.sys.version (): Get version information for Python interpreter
>>> sys.version'3.6.3 (default, OCT, 18:55:29) \n[gcc 4.8.5 20150623 (Red Hat 4.8.5 -11)]'
Python Common Module--sys module