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.打开隐藏 ...
随机推荐
- (EM算法)The EM Algorithm
http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006936.html http://blog.sina.com.cn/s/blog_a7da ...
- sql语句按照汉字拼音首字母排序
oracle : 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值SCHINESE_RADICAL_M 按照部首(第一顺序).笔划(第二顺序)排序SCHINESE_STR ...
- ERROR ITMS-90032 “Invalid image path”
在用 Application Loader上传spa 文件的时候出现这样的错误:ERROR ITMS-90032: "Invalid image path No image found at ...
- [Android Pro] Toolbar的完全自定义
reference to : http://blog.csdn.net/elder_sword/article/details/46634751 Toolbar是什么,不知道的可以去大神的博客瞻仰下 ...
- 关于each
1种 通过each遍历li 可以获得所有li的内容 <!-- 1种 --> <ul class="one"> <li>11a</li> ...
- T4模板
T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit. T4文本模板,即一种自定义规则的代码生成器.根据业务模型可生成任何形式的文本文件或供程序调 ...
- Eclipse的各种快捷键
参考他人总结好的 Eclipse快捷键大全(转载) Ctrl+1 快速修复(最经典的快捷键,就不用多说了) ---->例如:add unimplement methodCtrl+D: ...
- 按键的使用(一)------verilog
按键在项目中应用还是很频繁的,这里主要介绍按键的几种用法. 1.按下一次有效:按下一次计数器增加一下. 2.按下连续有效:按下不松,计数器就一直增加. 3.按下无效,松开有效:按下时计数器值不变,按键 ...
- Android Tab -- 使用TabWidget、TabHost、TabActivity来实现
原文地址:http://blog.csdn.net/crazy1235/article/details/42678877 TabActivity在API13之后被fragment替代了,所以不建议使用 ...
- mysql 查看用户的权限
show grants for 'username'@'%';