Programing Python--sys Module

Source: Internet
Author: User

Recall that every Python module have a built_in __name__ variable that Python sets to the __main__ string Only if the file is run as a program,not when it ' s imported as library

So in the Python fiel if __name__ = = "__main__": .... Is the top-level code

1 " "2 Aplit and interactively page a string of file of text3 " "4 5 defMore (text,numlines= 15):6Lines =str.splitlines (text)7      whilelines:8Chunk =Lines[:numlines]9Lines =Lines[numlines:]Ten          forLineinchChunkPrint(line) One         ifLines andRaw_input ('More ?') not inch['y','Y']: Break A  -  - if __name__=="__main__": the     ImportSYS -More (open (sys.argv[0)). Read (), 10)
View Code

When the more.py file is imported, we pass a explicit string to it more function,so this is the imported the More function

1  from Import  More 2 Import SYS 3 More (sys.  __doc__)
View Code

Introducing the SYS Module

Platforms and Versions

1 if ' win ':print ('hello Windows')2elif'  Lin':print ('Hello linux')
View Code

If You have code this must act differently on different machines, simply test the sys.platform string as done,

The Module Search Path

Sys.path is a list fo directory name strings representing the true search path in running Python interpreter when a modul E is imported Python scans this list from left to right,searching for the module's file on all directory named in the LIS T

The Sys.path list in simply initialized from your PYTHONPATH setting

Surprisingly,sys.path can actually is changed by program. Using Apend,extend,insert,pop,remove function

The Loaded Modules Table

Sys.modules is a dictionary containing one name:module entry for every module imported in your Python session

>>> Sys.modules

Excepstion detials

Other attribures in the SYS module allow us to fetch all the information related to the most recently Python exception,the Sys.exc_info function returns a tuple with the latest exception ' s Type,value and Traceback object

1 Try : 2     Raise Indexerror 3 except : 4     Print (Sys.exc_info ())
View Code

Other SYS Module exports:

Command_line arguments show up as a list of string called SYS.ARGV

Standard streams is available as Sys.stdin sys.stdout and Sys.stderr

Program exit can is forced with sys.exit calls

Programing Python--sys Module

Related Article

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.