python sys module

The sys module has many functions. The following introduces some more practical functions.

List of common functions of the sys module

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

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

  • sys.getdefaultencoding(): Get the current encoding of the system, generally the default is ascii.

  • sys.setdefaultencoding(): Set the default encoding of the system, this method will not be seen when executing dir(sys), if the execution fails in the interpreter, you can execute reload(sys) first, then execute setdefaultencoding('utf8'), at this time the system default encoding Set to utf8. (see Setting the System Default Encoding)

  • sys.getfilesystemencoding(): Get the encoding method used by the file system, return 'mbcs' under Windows, and return 'utf-8' under Mac.

  • sys.path: Get the string collection of the specified module search path, you can put the written module under a certain path obtained, and you can find it correctly when importing in the program.

  • sys.platform: Get the current system platform.

  • sys.stdin,sys.stdout,sys.stderrThe :stdin , stdout , and stderr variables contain stream objects corresponding to standard I/O streams. If you need more control over the output, and print doesn't do what you want, these are what you need. You can also replace them, this then you can redirect output and input to other devices, or handle them in non-standard ways

sys.argv

Function: Pass parameters from outside to inside the program
Example:sys.py

1 #!/usr/bin/env python
2 
3 import sys
4 print sys.argv[0]
5 print sys.argv[1]

run:

# python sys.py argv1
sys.py
argv1

sys.exit(n)

Function: Execute to the end of the main program, the interpreter automatically exits, but if you need to exit the program halfway, you can call the sys.exit function, with an optional integer parameter returned to the calling program, indicating that you can capture in the main program A call to sys.exit. (0 is normal exit, others are abnormal)

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 a certain path obtained, and you can find it correctly when importing in the program.

Example:

>>> import sys
>>> sys.path
['', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\lib', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36', 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python36\\site-packages', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']

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

sys.modules

Function: sys.modulesIt is a global dictionary, which is loaded in memory after python is started. Whenever a programmer imports a new module, sys.modulesthat module is automatically logged. When the module is imported again for the second time, python will look it up directly in the dictionary, thus speeding up the execution of the program. It has all the methods a dictionary has.

Example:modules.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

Function: stdin , stdout , and stderr variables contain stream objects corresponding to standard I/O streams. If you need more control over the output, and print doesn't do what you want, they're what you need. You can also replace them, Then you can redirect output and input to other devices, or handle them in non-standard ways

Original: http://www.cnblogs.com/cherishry/p/5725184.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324589567&siteId=291194637