Home > Article > Backend Development > What does sys in python mean?
The sys module has many functions. We introduce some more practical functions here. I believe you will like it. Come with me to explore the python module!
List of common functions of the sys module
sys.argv: implements passing parameters from outside the program to the program.
sys.exit([arg]): Exit in the middle of the program, arg=0 means normal exit.
sys.getdefaultencoding(): Gets the current encoding of the system, which generally defaults to ascii.
sys.setdefaultencoding(): Set the system default encoding. You will not see this method when executing dir (sys). If the execution fails in the interpreter, you can execute reload (sys) first, and then execute setdefaultencoding( 'utf8'), at this time the system default encoding is set to utf8. (See setting the system default encoding)
sys.getfilesystemencoding(): Gets the encoding used by the file system. It returns 'mbcs' under Windows and '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.stderr: The stdin, stdout, and stderr variables contain stream objects corresponding to the standard I/O streams. If you need more control over the output, and print does not satisfy you requirements, they are all you need. You can also replace them, in which case you can redirect output and input to other devices (device), or handle them in non-standard ways
sys.argv
Function: Pass parameters from outside to inside the program
Example: sys.py
import sys print sys.argv[0] print sys.argv[1]
Run
sys.py argv1
sys.exit(n)
Function: When the execution reaches the end of the main program, the interpreter automatically exits. However, if you need to exit the program midway, you can call the sys.exit function with an optional integer parameter returned to the program that called it, indicating that you can exit the program in the main program. The call to sys.exit is captured in the program. (0 means normal exit, others are abnormal)
Example: exit.py
import sys def exitfunc(value): print value sys.exit(0) print "hello" try: sys.exit(1) except SystemExit,value: exitfunc(value) print "come?"
Run
hello 1
The above is the detailed content of What does sys in python mean?. For more information, please follow other related articles on the PHP Chinese website!