50.sys module

Common list of functions sys module

  • sys.argv: Passing parameters to achieve the program from the external program.

  • sys.exit([arg]): The middle of the program exits, arg = 0 for the normal exit.

  • sys.getdefaultencoding(): Get the current coding system, usually the default is ascii.

  • sys.setdefaultencoding(): Set default encoding system, will not see this method when executed dir (sys), is not performed by the interpreter may be performed first reload (sys), performing setdefaultencoding ( 'utf8'), this time the system will default encoding is set to utf8. (See Setting system default encoding)

  • sys.getfilesystemencoding(): Get File encoding system returns 'mbcs' Windows, the return 'utf-8' under mac.

  • sys.path: Under a route acquisition module search path specified set of strings, may be obtained on written module, you can find the right time of import in the program.

  • sys.platform: Get the current system platform.

  • sys.stdin,sys.stdout,sys.stderr:.. Stdin, stdout, and stderr variable contains a standard I / O stream corresponding to the stream object needs to be better, if the control output, and the print can not meet your requirements, they require you that you can replace them, which when you can redirect the input and output to other devices (device), or in a non-standard manner thereof

sys.argv

Function: internal program parameters to pass externally
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

sys.argv [0] indicates that the program itself
sys.argv [1] represents a parameter program
sys.argv [2] of the second parameter indicates that the program

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
a=sys.argv[1]
b=sys.argv[2]
print(a)
print(b)
Examples of the above output: test1 Python ye.py Test 

Test 
test1

 

sys.exit(n)

Function: execution to the end of the main program, the interpreter automatically exit, but if you need to drop out program, you can call sys.exit function takes an optional integer parameter is returned to the calling program, that you can capture in the main program the call to sys.exit. (0 is normal exit, the other is abnormal)

Example:exit.py

Copy the code
#!/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?"
Copy the code

 

run:

# python exit.py
hello
1

 

sys.path

Function: Gets a collection of strings to specify the module search path, a path can be written under the module to get on, you can find the right time of import in the program.

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("自定义模块路径")

# ! / Usr / bin / env Python 
# - * - Coding: UTF-8 - * - 
Import SYS 
sys.path.append ( ' your module path ' )

 

sys.modules

Function: sys.modulesis a global dictionary that is after the python boot loader in memory. Whenever the programmer introducing new module sys.moduleswill automatically record the module. When the second re-import the module, python directly to the dictionary lookup, thereby speeding up the program running. It has all the dictionary methods have.

Example:modules.py

Copy the code
#!/usr/bin/env python

import sys

print sys.modules.keys()

print sys.modules.values()

print sys.modules["os"]
Copy the code 

run:

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

 

 

 

 

 

  • sys.argv: Passing parameters to achieve the program from the external program.

  • sys.exit([arg]): The middle of the program exits, arg = 0 for the normal exit.

  • sys.getdefaultencoding(): Get the current coding system, usually the default is ascii.

  • sys.setdefaultencoding(): Set default encoding system, will not see this method when executed dir (sys), is not performed by the interpreter may be performed first reload (sys), performing setdefaultencoding ( 'utf8'), this time the system will default encoding is set to utf8. (See Setting system default encoding)

  • sys.getfilesystemencoding(): Get File encoding system returns 'mbcs' Windows, the return 'utf-8' under mac.

  • sys.path: Under a route acquisition module search path specified set of strings, may be obtained on written module, you can find the right time of import in the program.

  • sys.platform: Get the current system platform.

  • sys.stdin,sys.stdout,sys.stderr:.. Stdin, stdout, and stderr variable contains a standard I / O stream corresponding to the stream object needs to be better, if the control output, and the print can not meet your requirements, they require you that you can replace them, which when you can redirect the input and output to other devices (device), or in a non-standard manner thereof

sys.argv

Function: internal program parameters to pass externally
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

sys.argv [0] indicates that the program itself
sys.argv [1] represents a parameter program
sys.argv [2] of the second parameter indicates that the program

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
a=sys.argv[1]
b=sys.argv[2]
print(a)
print(b)
Examples of the above output: test1 Python ye.py Test 

Test 
test1

 

sys.exit(n)

Function: execution to the end of the main program, the interpreter automatically exit, but if you need to drop out program, you can call sys.exit function takes an optional integer parameter is returned to the calling program, that you can capture in the main program the call to sys.exit. (0 is normal exit, the other is abnormal)

Example:exit.py

Copy the code
#!/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?"
Copy the code

 

run:

# python exit.py
hello
1

 

sys.path

Function: Gets a collection of strings to specify the module search path, a path can be written under the module to get on, you can find the right time of import in the program.

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("自定义模块路径")

# ! / Usr / bin / env Python 
# - * - Coding: UTF-8 - * - 
Import SYS 
sys.path.append ( ' your module path ' )

 

sys.modules

Function: sys.modulesis a global dictionary that is after the python boot loader in memory. Whenever the programmer introducing new module sys.moduleswill automatically record the module. When the second re-import the module, python directly to the dictionary lookup, thereby speeding up the program running. It has all the dictionary methods have.

Example:modules.py

Copy the code
#!/usr/bin/env python

import sys

print sys.modules.keys()

print sys.modules.values()

print sys.modules["os"]
Copy the code 

run:

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

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhangan/p/10966430.html