python重定向sys.stdin、sys.stdout和sys.stderr
转自:https://www.cnblogs.com/guyuyuan/p/6885448.html
标准输入、标准输出和错误输出。
标准输入:一般是键盘。stdin对象为解释器提供输入字符流,一般使用raw_input()和input()函数。
例如:让用户输入信息(Python环境为2.x):

1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 import sys
4 name = raw_input("Please input your name: ")
5 print name
6
7 # python test.py
8 Please input your name: xiaoming
9 xiaoming


1 import sys
2 print "Please enter your name: "
3 name = sys.stdin.readline()
4 print name
5
6 # python b.py
7 Please enter your name:
8 xiaoming
9 xiaoming

再例如,a.py文件标准输出作为b.py文件标准输入:

1 # cat a.py
2 import sys
3 sys.stdout.write("123456\n")
4 sys.stdout.flush()
5 # cat b.py
6 import sys
7 print sys.stdin.readlines()
8
9 # python a.py | python b.py
10 ['123456\n']

sys.stdout.write()方法其实就是下面所讲的标准输出,print语句就是调用了这个方法。
标准输出:一般是屏幕。stdout对象接收到print语句产生的输出。
例如:打印一个字符串:

1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 import sys
4 print "Hello world!"
5
6 # python test.py
7 Hello world!

sys.stdout是有缓冲区的,比如:

1 import sys
2 import time
3 for i in range(5):
4 print i,
5 # sys.stdout.flush()
6 time.sleep(1)
7 # python test.py
8 0 1 2 3 4

本是每隔一秒输出一个数字,但现在是循环完才会打印所有结果。如果把sys.stdout.flush()去掉,就会没执行到print就会刷新stdout输出,这对实时输出信息的程序有帮助。
错误输出:一般是错误信息。stderr对象接收出错的信息。
例如:引发一个异常
1 >>> raise Exception, "raise..."
2 Traceback (most recent call last):File "<stdin>", line 1, in <module>
3 Exception: raise...
总结:
sys.stdout与print
当我们在 Python 中打印对象调用 print obj 时候,事实上是调用了 sys.stdout.write(obj+'\n') ;print 将你需要的内容打印到了控制台,然后追加了一个换行符;print 会调用 sys.stdout 的 write 方法
以下两行在事实上等价:
1 sys.stdout.write('hello'+'\n')
2
3 print 'hello'
sys.stdin与raw_input:
当我们用 raw_input('Input promption: ') 时,事实上是先把提示信息输出,然后捕获输入
以下两组在事实上等价:
1 hi=raw_input('hello? ')
2
3 print 'hello? ', #comma to stay in the same line
4
5 hi=sys.stdin.readline()[:-1] # -1 to discard the '\n' in input stream
从控制台重定向到文件
原始的 sys.stdout 指向控制台
如果把文件的对象的引用赋给 sys.stdout,那么 print 调用的就是文件对象的 write 方法

1 f_handler=open('out.log', 'w')
2
3 sys.stdout=f_handler
4
5 print 'hello'
6
7 # this hello can't be viewed on concole
8
9 # this hello is in file out.log

记住,如果你还想在控制台打印一些东西的话,最好先将原始的控制台对象引用保存下来,向文件中打印之后再恢复 sys.stdout:

1 __console__=sys.stdout
2
3 # redirection start #
4
5 ...
6
7 # redirection end
8
9 sys.stdout=__console__

python重定向sys.stdin、sys.stdout和sys.stderr的更多相关文章
- Python中的sys.stdin和input、sys.stdout与print--附带讲解剑指offer42-连续子数组的最大和
2020秋招季,终于开始刷第一套真题了,整套试卷就一道编程题,还是剑指offer上的原题,结果答案死活不对,最后干脆直接提交答案算了,看了下别人的答案,原来是输入数据没有获取的原因,不过这个语法sys ...
- Python常用模块(time, datetime, random, os, sys, hashlib)
time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运 ...
- Python全栈之路----常用模块----sys模块
sys.argv 命令行参数 List,第一个元素是程序本身路径 #test.py import sys print(sys.argv) D:\ProgramLearning\Py_program& ...
- Python之路(第十五篇)sys模块、json模块、pickle模块、shelve模块
一.sys模块 1.sys.argv 命令行参数List,第一个元素是程序本身路径 2.sys.exit(n) 退出程序,正常退出时exit(0) 3.sys.version . sys.maxint ...
- python之sys.stdout、sys.stdin
转自:http://www.cnblogs.com/turtle-fly/p/3280519.html 本文环境:Python 2.7 使用 print obj 而非 print(obj) sys. ...
- python中sys.stdout、sys.stdin
如果需要更好的控制输出,而print不能满足需求,sys.stdout,sys.stdin,sys.stderr就是你需要的. 1. sys.stdout与print: 在python中调用print ...
- python之sys.stdout、sys.stdin以及设置打印到日志文件等
转自:https://www.cnblogs.com/BigFishFly/p/6622784.html python之sys.stdout.sys.stdin 转自:http://www.cnblo ...
- python 标准输入输出sys.stdout. sys.stdin
import sys, time ## print('please enter your name:')# user_input=sys.stdin.readline()# print(user_in ...
- python sys.stdin、sys.stdout和sys.stderr
学习并转载自 https://www.cnblogs.com/guyuyuan/p/6885448.html 标准输入:一般是键盘.stdin对象为解释器提供输入字符流,一般使用raw_input( ...
随机推荐
- 使用PHP生成和获取XML格式数据
1.php生成xml
- Oracle查询优化--单表查询
--查询所有 select * from emp; select * from emp where comm is null; --错误表达 --select * from emp where com ...
- 使用存储过程将Oracle数据批量导出为多个csv文件
数据库有如下表结构: user_info ( user_id NUMBER primary key, user_name VARCHAR2(200) NOT NUL ...
- mongodb group php 操作
紧接着上篇来,这篇主要讲,mongodb的group功能,做的还是挺强大的,相当对于find(),skip(),distinct()等,用法比较复杂. 测试数据 > db.fruit.find( ...
- linux,shell脚本中获取脚本的名字,使用脚本的名字。
需求描述: 写shell脚本的过程中,有时会需要获取脚本的名字,比如,有的时候,脚本 中会有usage()这种函数,可能就会用到脚本的名字. 实现方法: shell脚本中,通过使用$0就可以获取到脚本 ...
- 基于nodejs的开源博客
https://github.com/hexojs/hexo https://hexo.io/zh-cn/docs/ markdown编辑器 http://pandao.github.io/edito ...
- LeetCode——Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- swift -- 计步器CMPedometer的使用
最近公司接了个项目,是一款运动类型的APP,可以检测运动量(例如:步数,上下楼等).睡眠信息.速度等信息,因为以前粗略的了解过传感器方面的相关信息,知道主要是苹果设备内置的传感器在起作用,传感器的种类 ...
- ios 生成一个动态的随机的头像/随机数的操作
在写项目的时候,可能会遇到这种情况,用到集中随机的颜色,或者头像等, 首先:把所需要的图片放进一个数组当中 imgsAry = @[@"t1.png",@"t2.png& ...
- 泛型的几种类型以及初识winform
今天学习的可以分为两类吧,但是学习的都是比较抽象的,不太容易掌握吧.首先我们大部分时间学习了泛型,泛型的委托,泛型接口以及枚举器,迭代器,扩展方法:最后简单的认识了webform,实现了一个简单的功能 ...