python的sys模块
try statements are honored, and it is possible to intercept the exit attempt at an outer level. The optional argument
arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems
require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax
errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to sys.stderrand results in an exit code of 1. In particular, sys.exit("some
error message") is a quick way to exit a program when an error occurs.
#testing stdoutprint 'Hello World!'运行hello.py就会在标准输出的屏幕上打印 Hello World!, 我们再编一个简单的标准输入的小程序 sayhi.py:
#testing stdinprint 'Hi, %s!' % raw_input('Please enter your name:')当你用键盘输入你的名字后,程序在屏幕上输出Hi,[你的名字]!, 这就是从标准输入:键盘获取信息,再输出到标准输出:屏幕的例子。
那么上面的例子中print 和 raw_input是如何与标准输入/输出流建立关系的呢?
其实Python程序的标准输入/输出/出错流定义在sys模块中,分别 为: sys.stdin, sys.stdout, sys.stderr
上面的程序分别与下列的程序是一样的:
import syssys.stdout.write('Hello World!')import sysprint 'Please enter your name:',name=sys.stdin.readline()[:-1]print 'Hi, %s!' % name那么sys.stdin, sys.stdout, stderr到底是什么呢?我们在Python运行环境中输入以下代码:
import sysfor f in (sys.stdin, sys.stdout, sys.stderr): print f输出为:<open file '<stdin>', mode 'r' at 892210><open file '<stdout>', mode 'w' at 892270><open file '<stderr>', mode 'w at 8922d0>由此可以看出stdin, stdout, stderr在Python中无非都是文件属性的对象,他们在Python启动时自动与Shell 环境中的标准输入,输出,出错关联。
而Python程序的在Shell中的I/O重定向与本文开始时举的DOS命令的重定向完全相同,其实这种重定向是由Shell来提供的,与Python 本身并无关系。那么我们是否可以在Python程序内部将stdin,stdout,stderr读写操作重定向到一个内部对象呢?答案是肯定的。
Python提供了一个StringIO模块来完成这个设想,比如:
from StringIO import StringIO
import sys
buff =StringIO()
temp = sys.stdout #保存标准I/O流
sys.stdout = buff #将标准I/O流重定向到buff对象
print 42, 'hello', 0.001
sys.stdout =temp #恢复标准I/O流
print buff.getvalue()
python的sys模块的更多相关文章
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- python之sys模块
38.python的sys模块: 用于提供对Python解释器相关的操作: 1 2 3 4 5 6 7 8 9 sys.argv 命令行参数List,第一个元素是程序本身路径 sy ...
- Python中sys模块
Python的sys模块提供访问解释器使用或维护的变量,和与解释器进行交互的函数.通俗来讲,sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python运行时的环境. ...
- python学习笔记27(python中sys模块的使用)
sys.argv 命令行参数List,第一个元素是程序本身路径 sys.modules.keys() 返回所有已经导入的模块列表 sys.exc_info() 获取当前正在 ...
- 10 python os&sys 模块
1.os模块 os模块提供了很多允许你的程序与操作系统直接交互的功能 os模块的主要功能:处理文件和目录,系统相关,执行命令,管理进程 检验给出的路径是否是一个文件:os.path.isfile() ...
- Python 使用sys模块
你已经学习了如何在你的程序中定义一次函数而重用代码.如果你想要在其他程序中重用很多函数,那么你该如何编写程序呢?你可能已经猜到了,答案是使用模块.模块基本上就是一个包含了所有你定义的函数和变量的文 ...
- [ python ] 使用sys模块实现进度条
在写网络IO传输的时候, 有时候需要进度条来显示当前传输进度,使用 sys 模块就可以实现: sys.stdout.write() 这个函数在在控制台输出字符串不会带任何结尾,这就意味着这个输出还没有 ...
- Python OS & sys模块
os模块(* * * *) os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname" ...
- Python 的 sys 模块常用方法?
总结就是,os模块负责程序与操作系统的交互,提供了访问操作系统底层的接口; sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境. sys.argv ...
随机推荐
- IPFS星际文件系统
IPFS星际文件系统(InterPlanetary File System)是去中心化文件系统,本文介绍IPFS节点软件系统安装,环境搭建等简介入门教程,及学习如何使用ipfs-api和Node.js ...
- Bootstrap3 代码-程序输出
通过 <samp> 标签来标记程序输出的内容. This text is meant to be treated as sample output from a computer prog ...
- 关于在arm裸板编程时使用printf问题的解决方法
在ARM裸板驱动编程中,是不允许程序直接调用C库程序的.为什么呢?因为此时kernel还没有被加载,所以在封装在kernel层的C库的API是用不了的,那怎么办? 在开发过程中,printf的功能我不 ...
- Linux下文件和文件夹操作命令详解
花了两个小时的时间,把文件和文件夹相关的常用命令:创建.删除.移动.复制.查找.重命名在linux上测试了一把,总结下来.文件夹的这些基本操作是要多注意的,一不小心就达不到你想要的效果. 文件夹操作: ...
- String、StringBuffer、StringBuilder对比
1.String 用于存放字符的数组被声明为final的,因此只能赋值一次,不可再更改.这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间. Str ...
- Win7下安装linux虚拟机
关于如何在Win7下搭建linux学习环境,特在此分享下. 一.工具 1.VMware-workstation-full-9.0.0-812388.exe 下载地址:http://pan. ...
- Web Service进阶(三)HTTP-GET, HTTP-POST and SOAP的比较
XML Web Service支持三种协议来与用户交流数据.这三种协议分别是: 1.SOAP:Simple Object Access Protocol 2.HTTP-GET 3.HTTP-POST ...
- Dynamics CRM2016 New features in Microsoft Dynamics CRM Online 2015 Update 1 are now available
很多人看过Dynamics CRM Online 2015 Update 1后,被它新的一个界面风格所吸引,还有它的很多新增功能,虽然官网放出了些补丁,但最重要的Server补丁一直没出,包括我在内很 ...
- MAMP显示文件列表
背景 MAMP是Mac下的一个PHP+Nginx+MySQL的集成环境,支持多站点,不同版本PHP. 今天有人请教MAMP如何显示文件列表的问题,这里记录一下. 知识补充 一个网站为了安全考虑,默认是 ...
- 剑指Offer——银行网申内容模版
剑指Offer--银行网申内容模版 年获得"优秀共青团员"称号 2013.12 科技标兵 2013.10 三好学生 2013.10 "三下乡"优秀学生 2013 ...