python之sys.stdout、sys.stdin以及设置打印到日志文件等
转自:https://www.cnblogs.com/BigFishFly/p/6622784.html
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/ ...
- django设置打印数据库日志
在settings.py中添加: LOGGING = { 'disable_existing_loggers': False, 'version': 1, 'handlers': { 'console ...
- Python Linux 命令行执行脚本输出重定向print到日志文件
reference: https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately ...
- python之sys.stdout、sys.stdin
转自:http://www.cnblogs.com/turtle-fly/p/3280519.html 本文环境:Python 2.7 使用 print obj 而非 print(obj) sys. ...
- (转)Python标准库:内置函数print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
原文:https://blog.csdn.net/caimouse/article/details/44133241 https://www.cnblogs.com/owasp/p/5372476.h ...
- Python 内置库 sys用法
sys模块功能众多,这边先学习几个常用的方法sys常见函数列表① sys.argv: 实现从程序外部向程序传递参数.其实sys.argv[]就是一个列表,里面的项为用户输入的参数,但是sys.argv ...
随机推荐
- ubuntu 访问 共享 windows文件夹
sudo mount -o username=*******,password=******** //192.168.1.105/迅雷下载 /mnt/
- CentOS7.2内核版本查看简述
1.uname 命令 [root@bogon /]# uname --help 用法:uname [选项]... 输出一组系统信息.如果不跟随选项,则视为只附加-s 选项. -a, --all以如 ...
- Linux新手要了解的十个知识点
Linux对于有的新手来说,感觉无从下手,或者不知道从哪儿学起?怎么学?针对这些问题,我给大家说说新手学习Linux需要了解的十个知识点. 注意大小写 Linux是大小写敏感的系统,举个例子,Mozi ...
- MVC中导航菜单,选中项的高亮问题。。
先上图: 这个菜单是放在母板页的.比如当前选中的是异常业务监控.如果页面刷新了.就会变成第一张图..选择其他的选项也会,因为页面会刷新嘛.. 怎么处理这个问题了? 答案是记录当 ...
- [LeetCode] Subsets II [32]
题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Not ...
- Linux性能研究(总)
http://www.vpsee.com/2009/11/linux-system-performance-monitoring-introduction/ http://www.jb51.net/L ...
- xpath的数据和节点类型以及XPath中节点匹配的基本方法
XPath数据类型 XPath可分为四种数据类型: 节点集(node-set) 节点集是通过路径匹配返回的符合条件的一组节点的集合.其它类型的数据不能转换为节点集. 布尔值(boolean) ...
- Spring------Spring data jpa 定义实体类(@OneToMany等的使用)
转载: https://course.tianmaying.com/spring-data-jpa+one-to-many#2
- MySQL只有information_schema,test两个数据库
一.现象 1.今天登上数据库,用 mysql -uroot -proot 登录(本人密码是root),出现: 2.然后尝试 无密码登录,竟然登录成功: 3.查看mysql中的数据库,发现只有两个系统表 ...
- MUI 单图片压缩上传(拍照+系统相册): 选择立即上传
1 html 部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...