目标:

  1.使用sys.stdout.write输入0-9数字

  2.使用sys.stderr.write输出0-9数字

  3.使用两种方式输出0-9,显示0变化到9的过程

1.使用sys.stdout.write和sys.stderr.write打印

[root@localhost python]# cat 1.py
#!/usr/bin/env python import sys sys.stdout.write("stdout1")
sys.stderr.write("stderr1")
sys.stdout.write("stdout2")
sys.stderr.write("stderr2")
[root@localhost python]# python 1.py
stderr1stderr2stdout1stdout2[root@localhost python]#
提示:使用sys.stdout.write方式输出,顺序发生变化,输出2个stderr,再输出2个stdout,这是因为sys.stdout.write输出的方式有缓存,而sys.stderr.write输出的方式无缓存。
sys.stdout.write去掉缓存的方法:
  1.添加换行符
  2.执行脚本时添加 -u 选项
  3.调用sys.stdout.flush
①添加换行符'\n'
[root@localhost python]# cat 1.py
#!/usr/bin/env python import sys sys.stdout.write("stdout1\n")
sys.stderr.write("stderr1\n")
sys.stdout.write("stdout2\n")
sys.stderr.write("stderr2\n")
[root@localhost python]# python 1.py
stdout1
stderr1
stdout2
stderr2
②执行脚本加 -u 选项
[root@localhost python]# cat 1.py
#!/usr/bin/env python import sys sys.stdout.write("stdout1")
sys.stderr.write("stderr1")
sys.stdout.write("stdout2")
sys.stderr.write("stderr2")
[root@localhost python]# python 1.py
stdout1stderr1stdout2stderr2[root@localhost python]# ③调用sys.stdout.flush
[root@localhost python]# cat 1.py
#!/usr/bin/env python import sys sys.stdout.write("stdout1")
sys.stdout.flush()
sys.stderr.write("stderr1")
sys.stdout.write("stdout2")
sys.stdout.flush()
sys.stderr.write("stderr2")
[root@localhost python]# python 1.py
stdout1stderr1stdout2stderr2[root@localhost python]#

2.使用sys.stdout.write和sys.stderr.write输出0-9的变化过程

代码如下:

[root@localhost python]# cat std_write.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- import sys,time for i in xrange(10):
sys.stdout.write('%s\r' % i)
sys.stdout.flush()
time.sleep(0.2)
#打印换行
print for i in xrange(10):
sys.stderr.write('%s\r' % i)
time.sleep(0.2)
print

3.运行代码,测试效果,会看到0一直变化到9,最后显示9结束。

[root@localhost python]# python std_write.py
9
9

sys.stdout.write与sys.sterr.write(一)的更多相关文章

  1. sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码

    不论使用urllib还是使用requests库经常会遇到中文编码错误的问题,我就经常遇到,因为python安装在windows平台上,cmd的默认编码为GBK,所以在cmd中显示中文时会经常提示gbk ...

  2. python3.x设置默认编码(sys.stdout.encoding和sys.defaultencoding)

    查了一会资料得出的结论是如果你用的是python3.x,那么就最好别去设置sys.defaultencoding或者sys.stdout.encoding记住在需要编码的时候用encode,解码的时候 ...

  3. sys.stdout.write与sys.sterr.write(三)

    目标: 1.使用sys.stdout.write模拟"|"的顺时针变化- \ | / 2.使用sys.stderr.write模拟"|"的顺时针变化- \ | ...

  4. sys.stdout.write与sys.sterr.write(二)

    目标: 1.使用sys.stdout.write模拟火车道轨迹变化过程 2.使用sys.stderr.write模拟火车道轨迹变化过程 1.sys.stdout.write模拟火车道轨迹变化 代码如下 ...

  5. Python之print(args)与sys.stdout.write(string)使用总结

    一.sys.stdout.write(string) import sys; # sys.stdout.write(): # 1.默认不换行 # 2.参数必须是字符串 # demo 01 x = &q ...

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

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

  7. 【python】print · sys.stdout · sys.stderr

    参考文档 Python重定向标准输入.标准输出和标准错误 http://blog.csdn.net/lanbing510/article/details/8487997 python重定向sys.st ...

  8. PyQt(Python+Qt)学习随笔:print标准输出sys.stdout以及stderr重定向QTextBrowser等图形界面对象

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 <在Python实现print标准输出sys.stdout.st ...

  9. 在Python实现print标准输出sys.stdout、stderr重定向及捕获的简单办法

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 Python中的标准输出和错误输出由sys模块的stdout.stde ...

随机推荐

  1. VR系统的分类

    转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 根据用户参与和沉浸感的程度,通常把虚拟现实分为4大类:桌面虚拟现实系统.沉浸式虚拟现实系统.增强虚拟现实 ...

  2. 跨域调用webapi

    web端跨域调用webapi   在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webap ...

  3. Pyqt adb 获取Android手机屏幕

    adb的全称为Android Debug Bridge,就是起到调试桥的作用.adb的工作方式比较特殊,采用监听Socket TCP 5554等端口的方式让IDE和Qemu通讯,默认情况下adb会da ...

  4. WPF播放视频

    在现在的项目中需要使用到播放视频的功能,本来打算使用VLC来做的.后来发现WPF 4.0之后新增了MediaElement类,可以实现视频播放. <Grid> <Grid.RowDe ...

  5. 搭建eclipse+github开发环境

    开发环境 1.jdk:jdk1.8.0_60 2.eclipse:eclipse-jee-mars-R-win32-x86_64.zip 配置步骤 1.配置本地git目录(可选) eclipse-je ...

  6. 打造自己的php动态连接库文件

    http://blog.163.com/weibin_li/blog/static/1901464172012325115517181/

  7. xor和gates的专杀脚本

    前段时间的一次样本,需要给出专杀,应急中遇到的是linux中比较常见的两个家族gates和xor. 首先是xor的专杀脚本,xor样本查杀的时候需要注意的是样本的主进程和子进程相互保护(详见之前的xo ...

  8. JAVE not work in linux

    1, it will print out exception, but still can convert the audio 2, it works in windows not linux, ne ...

  9. linux install matlab2014a

    https://pan.baidu.com/s/1qYJ9tNm#list/path=%2F下载镜像文件 2#开始安装,全程最好断网 sudo mkdir /media/matlab sudo mou ...

  10. bigint数据类型

    尽管int依然是SQL Server 2000中最主要的整数数据类型,但是SQL Server 2000还是新增加了整数数据类型bigint,它应用于整数超过int数据范围的场合. int数据类型所表 ...