programing Python --Sys module
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
'''
aplit and interactively page a string of file of text
''' def more(text,numlines= 15):
lines = str.splitlines(text)
while lines:
chunk = lines[:numlines]
lines = lines[numlines:]
for line in chunk:print(line)
if lines and raw_input('more?') not in ['y','Y']:break if __name__ == "__main__":
import sys
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
from more import more
import sys
more(sys.__doc__)
Introducing the sys Module
PlatForms and Versions
if sys.platform[:3] == 'win':print ('hello windows')
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
try:
raise IndexError
except:
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的更多相关文章
- python no module named _socket 原因
python no module named _socket 原因 Lib/site-packages 不在 sys.path 中
- python sys.modules模块
sys.modules是一个全局字典,该字典是python启动后就加载在内存中.每当程序员导入新的模块,sys.modules都将记录这些模块.字典sys.modules对于加载模块起到了缓冲的作用. ...
- python模块module package
python模块module package module package 常用模块 模块与包的区别 模块分为内置模块.第三方模块,自定义模块 程序会先从内置到第三方再到当前工作目录下去找你导入的 ...
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
- Python sys.path.append
python sys.path.append 对于模块和自己写的程序不在同一个目录下,可以把模块的路径通过sys.path.append(路径)添加到程序中. 在程序开头加上: import syss ...
- Python——sys模块
七.sys模块 sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传递参数. sys.exit([arg]): 程序中间的退出,arg=0为正常退出. sys.getdefaulten ...
- Python “No module named” 以及在Python2中可以导入,但在python3中却出现的原因
Python “No module named” 以及在Python2中可以导入,但在python3中却出现的原因 原因之1: 例如有这样的一个包和它的模块: Test __init__.py Mod ...
- python sys模块(12)
在python sys模块提供对解释器使用或维护的一些变量的访问,以及与解释器强烈交互的函数!关于sys模块在官网也有详细的介绍:python sys模块官方介绍. 一.sys模块简介 sys.arg ...
- 问题1-/usr/bin/python: No module named virtualenvwrapper
操作系统:Ubuntu 问题:创建虚拟环境时,出现:/usr/bin/python: No module named virtualenvwrapper 解决方法: 1.切换到用户家目录 2.打开隐藏 ...
随机推荐
- 【opencv】轮廓相关
IplImage* contours_rect; IplImage* contours; CvMemStorage* countours_storage=NULL; if(contours_rect= ...
- IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
1.系统默认的颜色设置 [cpp] view plaincopy //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 ...
- php date函数 参数详细
time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计算, 要找出前一天的时间就是 time()-60*60*24; 要找出前一年的时间就 ...
- html5手机网站需要加的那些meta/link标签,html5 meta全解
原文链接:http://blog.csdn.net/kongjiea/article/details/17092413(收藏专用!如需转载,请点击链接,联系博主,获得同意后方可转载) 3.name之设 ...
- eclipse上安装abator插件
下面是我看了网上的有一点需要强调:网址 http://ibatis.apache.org/tools/abator然后全选,然后是==>重启就好了 eclipse上安装abator插件参考:ht ...
- NYOJ之喷水装置(一)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsUAAAJvCAIAAAAcLjvHAAAgAElEQVR4nO3drXLjzNaG4e8kzH0gof
- AngularJS XMLHttpRequest $http服务
$http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据. 读取JSON文件 以下是存储在web服务器上的 JSON 文件: http://www.runoob.com/try/ ...
- javascript - 内置对象 String/Date/Array/Math
1.构建对象的方法 <script> //构建对象方法 //第1种 var people = new Object(); people.name = "iwen"; p ...
- 常用的数据统计Sql 总结(转)
转:http://www.cnblogs.com/zhangweizhong/p/5577842.html 最近刚在搞一个BI的项目,里面需要大量的sql 数据统计相关运用,加深了我又对SQL的理解与 ...
- HTML5应用之文件拖拽上传
使用HTML5的文件API,可以将操作系统中的文件拖放到浏览器的指定区域,实现文件上传到服务器.本文将结合实例讲解HTML5+jQuery+PHP实现拖拽上传图片的过程,来看下HTML5的魅力吧. H ...