转自: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的更多相关文章

  1. Python中的sys.stdin和input、sys.stdout与print--附带讲解剑指offer42-连续子数组的最大和

    2020秋招季,终于开始刷第一套真题了,整套试卷就一道编程题,还是剑指offer上的原题,结果答案死活不对,最后干脆直接提交答案算了,看了下别人的答案,原来是输入数据没有获取的原因,不过这个语法sys ...

  2. Python常用模块(time, datetime, random, os, sys, hashlib)

    time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) :         通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运 ...

  3. Python全栈之路----常用模块----sys模块

    sys.argv  命令行参数 List,第一个元素是程序本身路径 #test.py import sys print(sys.argv) D:\ProgramLearning\Py_program& ...

  4. Python之路(第十五篇)sys模块、json模块、pickle模块、shelve模块

    一.sys模块 1.sys.argv 命令行参数List,第一个元素是程序本身路径 2.sys.exit(n) 退出程序,正常退出时exit(0) 3.sys.version . sys.maxint ...

  5. python之sys.stdout、sys.stdin

    转自:http://www.cnblogs.com/turtle-fly/p/3280519.html 本文环境:Python 2.7  使用 print obj 而非 print(obj) sys. ...

  6. python中sys.stdout、sys.stdin

    如果需要更好的控制输出,而print不能满足需求,sys.stdout,sys.stdin,sys.stderr就是你需要的. 1. sys.stdout与print: 在python中调用print ...

  7. python之sys.stdout、sys.stdin以及设置打印到日志文件等

    转自:https://www.cnblogs.com/BigFishFly/p/6622784.html python之sys.stdout.sys.stdin 转自:http://www.cnblo ...

  8. python 标准输入输出sys.stdout. sys.stdin

    import sys, time ## print('please enter your name:')# user_input=sys.stdin.readline()# print(user_in ...

  9. python sys.stdin、sys.stdout和sys.stderr

    学习并转载自  https://www.cnblogs.com/guyuyuan/p/6885448.html 标准输入:一般是键盘.stdin对象为解释器提供输入字符流,一般使用raw_input( ...

随机推荐

  1. Floyd算法思想

    关键词:代数.图论.矩阵.松弛技术.动态规划 Floyd算法是一个经典的动态规划算法.用通俗的语言来描述的话,首先我们的目标是寻找从点i到点j的最短路径.从动态规划的角度看问题,我们需要为这个目标重新 ...

  2. hoj Counting the algorithms

    贪心加树状数组 给出的数据可能出现两种情况,包括与不包括,但我们从右向左删就能避免这个问题. #include<stdio.h> #include<string.h> #inc ...

  3. Linux新手要了解的十个知识点

    Linux对于有的新手来说,感觉无从下手,或者不知道从哪儿学起?怎么学?针对这些问题,我给大家说说新手学习Linux需要了解的十个知识点. 注意大小写 Linux是大小写敏感的系统,举个例子,Mozi ...

  4. js unicode处理

    //岗位详细界面 var str="1.\u000D\u21B52.\u000D\u21B53"; var pad = function() { var tbl = []; ret ...

  5. AssetBundle之LoadFromCacheOrDownload()取代"new WWW (url)的作用

    1.为了充分利用缓存功能.2.缓存的资源包完全由文件名和版本号唯一标识,URL所有域名和路径信息在缓存被忽略.3.由于缓存的资源包通过文件名而不是完整的URL来识别,在任何时候资源包被下载,你可以更改 ...

  6. shell脚本中,将所有的参数值否赋给一个变量或者说将所有的参数合成一个字符串,获取所有参数

    需求描述: 在写脚本的过程中,遇到这样的一个需求,将脚本执行过程中,传递给 脚本的所有的参数,都赋值给一个变量然后在对这个变量进行处理. 测试过程: 通过以下的脚本将所有传递给脚本的变量都赋值一个变量 ...

  7. mysql数据库,如何在登录mysql之后执行操作系统上的SQL脚本?

    需求描述: 通过mysql客户端登录到mysql数据库,如何执行操作系统上的SQL脚本文件呢? 操作过程: 1.编写测试脚本文件 [mysql@redhat6 scripts]$ cat SeCoun ...

  8. ubuntu普通用户无法使用usdo命令

    1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命令是: chmod u+w /etc/sudoers 3.编辑sudoers文件 vi /etc/s ...

  9. C#的字符串优化-String.Intern、IsInterned

    https://www.jianshu.com/p/af6eb8d3d4bf 首先看一段程序: using System; class Program { static void Main(strin ...

  10. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)

    使用maven打包的时候出现如下错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compil ...