Some common standard libraries of python
1. sys is a module used to interact with the python interpreter. Sys. argv is used to pass parameters to the python interpreter, also known as "command line parameters ". The Code is as follows:
#coding=utf-8import sysprint "The file name:",sys.argv[0]print "The number of arg:",len(sys.argv)print "Thr arg is:",str(sys.argv)
Run: python 123.py 1 2 3
The file name: 123.pyThe number of arg: 4www.bkjia.comThr arg is: ['123.py', '1', '2', '3']
Sys. exit () is used to exit the current program. If return is different from return, return returns a value, and sys. exit () returns an exception SystemExit. The Code is as follows:
#coding=utf-8import sysfor i in range(10): if i == 5: sys.exit() else: print i
Execution result: 123450
Note that sys. exit (0) indicates normal exit; or sys. exit ("there is something error") to prompt sys. path: PATH of the python interpreter search module. Similar to the path variable in linux, It is a list. You can use append () to add a PATH.
Sys. stdin, sys. stdout, sys. stderr standard input and standard output. The standard error code is as follows:
>>> import sys>>> out = sys.stdout>>> f = open("/tmp/sadas","w")>>> sys.stdout = f>>> print 1>>> print 2>>> print 3>>> f.close()>>> sys.stdout = out>>> print 44>>> print 55
Note that if we only need to write some stdout to a file, we must assign the stdout value to a variable first.