python之sys.stdout、sys.stdin
转自:http://www.cnblogs.com/turtle-fly/p/3280519.html
本文环境:Python 2.7
使用 print obj 而非 print(obj)
sys.stdin,sys.stdout,sys.stderr: stdin , stdout , 以及stderr 变量包含与标准I/O 流对应的流对象. 如果需要更好地控制输出,而print 不能满足你的要求, 它们就是你所需要的. 你也可以替换它们, 这时候你就可以重定向(script.py < file.txt>)输出和输入到其它设备( device ), 或者以非标准的方式处理它们
1.1 sys.stdout 与 print
当我们在 Python 中打印对象调用 print obj 时候,事实上是调用了 sys.stdout.write(obj+'\n')
print 将你需要的内容打印到了控制台,然后追加了一个换行符
print 会调用 sys.stdout 的 write 方法
以下两行在事实上等价:
sys.stdout.write('hello'+'\n')
print 'hello'
1.2sys.stdin 与 raw_input
当我们用 raw_input('Input
promption: ') 时,事实上是先把提示信息输出,然后捕获输入
以下两组在事实上等价:
hi=raw_input('hello? ')
print 'hello? ', #comma to stay in the same line
hi=sys.stdin.readline()[:-1]
# -1 to discard the '\n' in input stream
1.3从控制台重定向到文件
原始的 sys.stdout 指向控制台
如果把文件的对象的引用赋给
sys.stdout,那么 print 调用的就是文件对象的 write 方法
f_handler=open('out.log', 'w')
sys.stdout=f_handler
print 'hello'
# this hello can't be viewed on concole
# this hello is in file out.log
记住,如果你还想在控制台打印一些东西的话,最好先将原始的控制台对象引用保存下来,向文件中打印之后再恢复 sys.stdout
__console__=sys.stdout
# redirection start #
...
# redirection end
sys.stdout=__console__
1.4同时重定向到控制台和文件
如果我们希望打印的内容一方面输出到控制台,另一方面输出到文件作为日志保存,那么该怎么办?
将打印的内容保留在内存中,而不是一打印就将
buffer 释放刷新,那么放到一个字符串区域中会怎样?
a=''
sys.stdout=a
print 'hello'
OK,上述代码是无法正常运行的
Traceback (most recent call last): File
".\hello.py", line xx, in print 'hello'
AttributeError: 'str'
object has no attribute 'write'
错误很明显,就是上面强调过的,在尝试调用
sys.stdout.write() 的时候,发现没有 write 方法
另外,这里之所以提示
attribute error 而不是找不到函数等等,我猜想是因为python 将对象/类的函数指针记录作为对象/类的一个属性来对待,只是保留了函数的入口地址
既然这样,那么我们必须给重定向到的对象实现一个 write
方法:
import sys
class __redirection__:
def __init__(self):
self.buff=''
self.__console__=sys.stdout
def write(self, output_stream):
self.buff+=output_stream
def to_console(self):
sys.stdout=self.__console__
print self.buff
def to_file(self, file_path):
f=open(file_path,'w')
sys.stdout=f
print self.buff
f.close()
def flush(self):
self.buff=''
def reset(self):
sys.stdout=self.__console__
if __name__=="__main__":
# redirection
r_obj=__redirection__()
sys.stdout=r_obj
# get output stream
print 'hello'
print 'there'
# redirect to console
r_obj.to_console()
# redirect to file
r_obj.to_file('out.log')
# flush buffer
r_obj.flush()
# reset
r_obj.reset()
同样的,sys.stderr, sys.stdin 也都可以被重定向到多个地址
python之sys.stdout、sys.stdin的更多相关文章
- 【python】print · sys.stdout · sys.stderr
参考文档 Python重定向标准输入.标准输出和标准错误 http://blog.csdn.net/lanbing510/article/details/8487997 python重定向sys.st ...
- python 标准输入输出sys.stdout. sys.stdin
import sys, time ## print('please enter your name:')# user_input=sys.stdin.readline()# print(user_in ...
- sys.stdout sys.stderr的用法
stdout:标准输出 stderr:标准错误 print 相当于 sys.stdout.write() + 换行 一个将数据流写入文件的程序,文件名为:main.py def main(out=s ...
- Python sys.stdout sys.stdin
引用自:https://www.cnblogs.com/keye/p/7859181.html 引用自:https://blog.csdn.net/sxingming/article/details/ ...
- python之sys.stdout、sys.stdin以及设置打印到日志文件等
转自:https://www.cnblogs.com/BigFishFly/p/6622784.html python之sys.stdout.sys.stdin 转自:http://www.cnblo ...
- Python中os与sys两模块的区别
<os和sys的官方解释> ➤os os: This module provides a portable way of using operating system dependent ...
- Python 内置库 sys用法
sys模块功能众多,这边先学习几个常用的方法sys常见函数列表① sys.argv: 实现从程序外部向程序传递参数.其实sys.argv[]就是一个列表,里面的项为用户输入的参数,但是sys.argv ...
- Python基本模块介绍:sys、platform
sys模块 常用函数 sys.argv 命令行参数,实现从程序外部向程序传递参数. sys.path 模块搜索路径. sys.platform 获取当前系统平台. sys.version 获取pyth ...
- python 基础 7.6 sys 模块
一.sys 模块 sys 模块主要功能是获取参数 [root@www pythonscripts]# cat 2.py #!/usr/bin/python #coding=utf-8 im ...
随机推荐
- html5 canvas 奇怪的形状水平渐变(因为大多数的之前的文章把基础都打过了,所以开始写的快了,如果有不明白的,可以回顾下之前的)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 20155210潘滢昊 2016-2017-2 《Java程序设计》第8周学习总结
20155210 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 认识NIO Channel: 衔接数据节点(与IO中的流对比) isOpen close R ...
- [BZOJ 1032][JSOI 2007]祖玛 题解(区间DP)
[BZOJ 1032][JSOI 2007]祖玛 Description https://www.lydsy.com/JudgeOnline/problem.php?id=1032 Solution ...
- 一个网页的对象抽象之路——po编程 (干货,Java自动化测试)
先来看一个在腾讯课堂首页搜索机构的操作步骤: 1:首先打开腾讯课堂的首页:https://ke.qq.com 2:点击课程或机构的下拉选择图标 3:选择机构 4:在搜索框输入要搜索的机构名称 5:点击 ...
- 网页排版的时候不要忘了table标签
[概况] DIV+CSS是WEB设计标准,它是一种网页的布局方法.与传统中通过表格(table)布局定位的方式不同,它可以实现网页页面内容与表现相分离.但有时候在布局的时候,纯粹的用div感觉嵌套的太 ...
- csfb
SELECT CSDBTOGSMALLSuccessRate,CSFBTOGSMMODelay,CSFBTOGSMMODropRate,CSFBTOGSMMOFRStartCount,CSFBTOGS ...
- MySQL用户密码过期登陆报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
今天接到主从复制失败告警,查看MySQL,发现MySQL能够登陆但是执行命令报错, ERROR 1820 (HY000): You must reset your password using ALT ...
- Visual Studio 2013/2015/2017快捷键(转)
英文原文:19 Must-Know Visual Studio Keyboard Shortcuts 项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = ...
- webpack2.0学习
1.进到指定的目录下,新建自己的文件名 mkdir webpack-test 创建你的项目名称或者你己有的项目名称cd webpack-test npm initnpm install webpack ...
- ajax最基础入门
1.介绍 AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重 ...