programing Python --Sys module

來源:互聯網
上載者:User

標籤:

 Recall that every python module has a built_in __name__ variable that python sets to the __main__ string only when 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 text 3 ‘‘‘ 4  5 def more(text,numlines= 15): 6     lines = str.splitlines(text) 7     while lines: 8         chunk = lines[:numlines] 9         lines = lines[numlines:]10         for line in chunk:print(line)11         if lines and raw_input(‘more?‘) not in [‘y‘,‘Y‘]:break12 13 14 if __name__ == "__main__":15     import sys16     more(open(sys.argv[0]).read(),10)
View Code

when the more.py file is imported ,we pass an explicit string to its more function,so this is the imported way to run the more function

1 from more import more2 import sys3 more(sys.__doc__)
View Code

Introducing the sys Module

   PlatForms and Versions

1 if sys.platform[:3] == ‘win‘:print (‘hello windows‘)2 elif sys.platform[:3] == ‘lin‘:print (‘hello linux‘)
View Code

If you have code that 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 module is imported python scans this list from left to right,searching for the module‘s file on each directory named in the list,

the sys.path list in simply initialized from your PYTHONPATH setting

Surprisingly,sys.path can actually be 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 IndexError3 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 are available as sys.stdin sys.stdout and sys.stderr

       program exit can be forced with sys.exit calls

programing Python --Sys module

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.