The SYS module includes a very useful set of services, including a number of function methods and variables, to handle Python runtime configuration and resources so that it can interact with system environments other than the previous program, such as the Python interpreter.
(1) SYS.ARGV implementation of parameters passed from outside the program to the program
The SYS.ARGV variable is a list of strings that contain command-line arguments, and the command line is used to pass parameters to the program. Where the name of the script is always the first parameter of the SYS.ARGV list.
import sys print (SYS.ARGV) if len (sys.argv) > 1: print (sys.argv[ 0]) print (Sys.argv[1 print (Sys.argv[2 else : print (" No parameters passed ")
(2) Sys.path contains the list of directory names for the input modules.
Gets a collection of strings for the specified module's search path, which can be placed in a given path, and can be found correctly when import is in the program. When import imports Module_name, the Sys.path path is used to search for module.name, or you can customize the Add module path.
Sys.path.append ("Custom module Path")
Import SYS Print (Type (sys.path)) for inch Sys.path: Print (i)
(3) Sys.exit ([arg]) Program Intermediate exit, arg=0 for normal exit
Normally executes 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 program that called it, indicating that you can capture the call to Sys.exit in the main program. (0 is normal exit, other exceptions) of course, you can also use a string parameter, indicating the error message is unsuccessful.
(4) Sys.modules
Sys.modules is a global dictionary that is loaded in memory when Python is started. Whenever a programmer imports a new module, Sys.modules will automatically record the module. When the module is re-imported the second time, Python will look directly into the dictionary, speeding up the program's speed. It has all the methods that the dictionary has.
(5) Sys.getdefaultencoding ()/sys.setdefaultencoding ()/sys.getfilesystemencoding ()
Sys.getdefaultencoding ()
Gets the system current encoding, which is generally ASCII by default.
Sys.setdefaultencoding ()
Set the system default encoding, do not see this method when executing dir (SYS), do not pass in the interpreter, can execute reload (SYS), perform setdefaultencoding (' UTF8 ') at this time, the system default encoding is set to UTF8. (See Set system default encoding)
Sys.getfilesystemencoding ()
Get file system using encoding method, return ' MBCS ' under Windows, Mac back ' Utf-8 '
(6) Sys.stdin, Sys.stdout, Sys.stderr
stdin, stdout, and stderr variables contain stream objects that correspond to standard I/O streams. If you need more control over the output, and print does not meet your requirements, they are what you need. You can also replace them by redirecting output and input to other devices or handling them in a non-standard way.
(7) Sys.platform
Gets the current system platform. such as: Win32, Linux and so on.
Python sys module