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. p> li> -
sys. Exit ([arg]) code> : 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. Getfilesystemencoding () code> : access to the file system using encoding, return the 'MBCS' Windows, MAC returns' utf-8. p> li>
-
sys. Path code> : 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. p> li>
-
sys. Platform code> : access to current system platform. p> li>
-
sys. Stdin, sys. Stdout, sys. Stderr code> : 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.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) p> li>
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:
- An introduction to sys modules in Python and how packages are imported and used
- Python defines a full vector class
- [Solved] RuntimeError (note: full exception trace is shown but execution is paused at: <module>)
- [example multitasking] Python multithreading module
- Python 3 uses the relative path import module
- Detailed explanation of OpenCV approxpolydp() function
- [Solved] Python Project Import Module Error: ModuleNotFoundError
- ModuleNotFoundError: No module named ‘tensorflow.python’ And the pits encountered after installation
- Importerror: DLL load failed: unable to find the specified module in Python
- Python: How to Solve multiprocessing module Error in Windows
- How to Solve Python AttributeError: ‘module’ object has no attribute ‘xxx’
- Solution for Python3.7 import gevent module error
- Python AttributeError: module ‘tensorflow‘ has no attribute ‘InteractiveSession‘
- [Solved] R Error: Python module tensorflow.keras was not found.
- Python3.7 AttributeError: module ‘time‘ has no attribute ‘clock‘
- Python Pandas Error: No module named ‘openpyxl‘
- Python Import Error: SystemError: Parent module ‘‘ not loaded, cannot perform relative import
- [Solved] Python Error: AttributeError: partially initialized module ‘keyword‘ has no attribute ‘kwlist‘
- How to Solve Yolox Training C Disk Full Issue
- The solution of no such file or directory and cannot load native module running error of python3 pyinstaller after packaging