Full explanation of SYS module of Python

sys module has many functions, here we introduce some more practical functions, I believe you will like it, and I will walk into the python module!

sys module list of common functions

  • sys.argv: implements passing parameters from outside the program to the program.

  • sys. Exit ([arg]) : in the middle of the program exit, arg = 0 as normal exit.

  • sys.getdefaultencoding(): gets the current code of the system, which is generally ASCII by default.

  • sys.setdefaultencoding(): set the default code of the system. This method will not be seen when dir (sys) is executed. It cannot be executed in the interpreter. (see set the system default encoding)

  • sys. Getfilesystemencoding () : access to the file system using encoding, return the 'MBCS' Windows, MAC returns' utf-8.

  • sys. Path : Gets a collection of strings that specify the module search path. You can place the written module in one of the resulting paths and find it correctly when importing in your program.

  • sys. Platform : access to current system platform.

  • sys. Stdin, sys. Stdout, sys. Stderr : stdin, stdout, and stderr variable contains corresponding with the standard I/O flow stream objects. If you need more control over the output, and print doesn't give you what you want, that's all you need. You can also replace them by redirecting output and input to other devices, or by processing them in a non-standard way

sys.argv

function: pass parameters from outside to inside the program
example: sys.py

#!/usr/bin/env python

import sys
print sys.argv[0]
print sys.argv[1]

run:

# python sys.py argv1
sys.py
argv1

try it yourself and understand the corresponding parameter

sys.exit(n)

function: at the end of the main program, the interpreter exits automatically, but if you need to exit the program, you can call sys.exit with an optional integer argument returned to the calling program, indicating that you can capture a call to sys.exit from the main program. (0 is normal exit, others are exceptions)

example: exit.py

#!/usr/bin/env python

import sys

def exitfunc(value):
    print value
    sys.exit(0)

print "hello"

try:
    sys.exit(1)
except SystemExit,value:
    exitfunc(value)

print "come?"

run:

# python exit.py
hello
1

sys.path

function: get the string collection of the specified module search path, you can put the written module under the path, it can be found correctly in the program import.

example:

>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

sys.path. Append (" custom module path ")

sys.modules

is a global dictionary that is loaded in memory after python is started. Every time a programmer imports a new module, sys.modules will automatically record the module. When the module is imported a second time, Python looks it up directly in the dictionary, which speeds up the program. It has everything a dictionary has.

Py

#!/usr/bin/env python

import sys

print sys.modules.keys()

print sys.modules.values()

print sys.modules["os"]

run:

python modules.py
['copy_reg', 'sre_compile', '_sre', 'encodings', 'site', '__builtin__',......

sys.stdin\stdout\stderr

features: stdin, stdout, and stderr variables contain flow objects corresponding to standard I/O flows. If you need more control over the output, and print doesn't give you what you want, that's all you need. You can also replace them by redirecting output and input to other devices, or by processing them in a nonstandard way

Don't forget the original intention, always put


Read More: