Some common standard libraries of python

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.