标签:
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‘]:break 12 13 14 if __name__ == "__main__": 15 import sys 16 more(open(sys.argv[0]).read(),10)
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 more 2 import sys 3 more(sys.__doc__)
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‘)
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 IndexError 3 except: 4 print (sys.exc_info())
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
标签:
原文地址:http://www.cnblogs.com/someoneHan/p/4638285.html