python stdin和stdout_python重定向sys.stdin、sys.stdout和sys.stderr

转自:https://www.cnblogs.com/guyuyuan/p/6885448.html

标准输入、标准输出和错误输出。

标准输入:一般是键盘。stdin对象为解释器提供输入字符流,一般使用raw_input()和input()函数。

例如:让用户输入信息(Python环境为2.x):

33eecab1ad5c1c5b1bc32394cb0d17bf.gif

1 #!/usr/bin/python

2 #-*- coding: utf-8 -*- 3 importsys 4 name = raw_input("Please input your name: ") 5 printname 6 7 #python test.py 8 Please input your name: xiaoming 9 xiaoming

26d7f33d05d62ab3f1c041fefa928ba1.gif

a0d2283f6ab45c1f88b99eae82bd81ca.gif

1 importsys

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

9e3af9ae003a70d2fe98f71ce1b98186.gif

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

f85cff1dc511ad2442817435530862d5.gif

1 #cat a.py

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

d7a3aa4358e5275ddee75fd32037bfb2.gif

sys.stdout.write()方法其实就是下面所讲的标准输出,print语句就是调用了这个方法。

标准输出:一般是屏幕。stdout对象接收到print语句产生的输出。

例如:打印一个字符串:

4ca8496359ecfcf251232f4d0f0ffdaa.gif

1 #!/usr/bin/python

2 #-*- coding: utf-8 -*- 3 importsys 4 print "Hello world!" 5 6 #python test.py 7 Hello world!

a3aa088bb056c5d34db2de110e9c9365.gif

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

a4070ddbea105ed572c1d90aee08f260.gif

1 importsys

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

c7eff6ebec405636d64b2ee028bcc606.gif

本是每隔一秒输出一个数字,但现在是循环完才会打印所有结果。如果把sys.stdout.flush()去掉,就会没执行到print就会刷新stdout输出,这对实时输出信息的程序有帮助。

错误输出:一般是错误信息。stderr对象接收出错的信息。

例如:引发一个异常

1 >>> raise Exception, "raise..." 2 Traceback (most recent call last):File "", line 1, in 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 方法

96677a8f0fc06a1629b87885ebfe7402.gif

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

ae9d500dc222f44e6aab3bb85803a786.gif

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

00c05e169b673f752ef7d70720ca7f0e.gif

1 __console__=sys.stdout

2

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

dd616fa78c198bee78e1d49c4a6a11f7.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值