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.打开隐藏 ...
随机推荐
- 【processing】小代码
今天无意间发现的processing 很有兴趣 实现很简洁 void setup(){ } void draw(){ background(); && mouseY > heig ...
- IOS - ARC改为非ARC
1.project -> Build settings -> Apple LLVM complier 3.0 - Language -> objective-C Automatic ...
- IOS- 应用程序生命周期(前后台切换,应用的各种状态)详解
iOS的应用程序的生命周期,还有程序是运行在前台还是后台,应用程序各个状态的变换,这些对于开发者来说都是很重要的. iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受 ...
- TFS强制解除签入签出锁定
首先你要知道你服务器的用户名和登录密码 看被别人签出了的文件
- struts.xml配置
1. package标签 package:完成有业务相关的Action(应用控制器的)管理 name:给包起的名字(反映该包中Action的功能),用来完成包和包之间的继承.默认继承struts-de ...
- 利用颜色生成UIImage
// 颜色转换为背景图片 + (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1. ...
- 解决git客户端MINGW32下的“Could not open a connection to your authentication agent.”
使用git, 下载客户端后想进行和github 进行ssh 互通 出现以下情况: hadoop@deng-PC MINGW32 ~/.ssh$ ssh-add ~/.ssh/id_rsaCould n ...
- MySQL 监控
•Table_locks_immediate The number of times that a request for a table lock could be granted immedia ...
- VS使用技巧(转)
转自http://www.cnblogs.com/xpvincent/p/3596553.html i. Ctrl-M-O 折叠所有方法 ii. Ctrl-M-P 展开所有方法并停止大纲显示(不可以再 ...
- Python 打包程序判断是否已经运行
代码如下: # -*- coding: UTF8 -*- from win32com.client import Dispatch import win32com import sys, os fro ...