sys.stdout.write和print和sys.stdout.flush
1. 先看下官方文档
"""
sys.stdout.write(string)
Write string to stream.
Returns the number of characters written (which is always equal to the length of the string). print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
可以看出
①sys.stdout.write是将str写到流,原封不动,不会像print那样默认end='\n'
②sys.stdout.write只能输出一个str,而print能输出多个str,且默认sep=' '(一个空格)
③print,默认flush=False.
④print还可以直接把值写到file中
import sys
f = open('test.txt', 'w')
print('print write into file', file=f)
f.close()
2. sys.stdout.flush()
flush()
method of _io.TextIOWrapper instance
Flush write buffers, if applicable. This is not implemented for read-only and non-blocking streams.
flush是刷新的意思,在print和sys.stdout.write输出时是有一个缓冲区的。
比如要向文件里输出字符串,是先写进内存(因为print默认flush=False,也没有手动执行flush的话),在close文件之前直接打开文件是没有东西的,如果执行一个flush就有了。
import time
import sys for i in range(5):
print(i)
sys.stdout.flush()
time.sleep(1)
在终端执行上面代码,会一秒输出一个数字。然而如果注释掉flush,就会在5秒后一次输出01234
sys.stdout.write和print和sys.stdout.flush的更多相关文章
- python 中sys.stdout.write 和 print >> sys.stdout的区别(转)
下面应该可以解你的惑了: print >> sys.stdout的形式就是print的一种默认输出格式,等于print "%VALUE%" 看下面的代码的英文注释,是p ...
- [Python]print vs sys.stdout.write
之前只是在项目中看到过,没怎么注意,正好跟对象一起看python学习手册,看到了这个部分于是来研究下. python版本 2.7.x os win7 print 一般就是执行脚本的时候,把信息直接 ...
- PyQt(Python+Qt)学习随笔:print标准输出sys.stdout以及stderr重定向QTextBrowser等图形界面对象
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 <在Python实现print标准输出sys.stdout.st ...
- 在Python实现print标准输出sys.stdout、stderr重定向及捕获的简单办法
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 Python中的标准输出和错误输出由sys模块的stdout.stde ...
- python 2.x 中print >> sys.out ,print 与sys.out.write()的关系
print 会调用 sys.stdout 的 write 方法 以下两行在事实上等价: sys.stdout.write('hello,python'+'\n') print 'hello,pytho ...
- print和sys.stdout
print print语句执行的操作是一个写操作,把我们从外设输入的数据写到了stdout流,并进行了一些特定的格式化.和文件方法不同,在执行打印操作是,不需要将对象转换为字符串(print已经帮我们 ...
- 关于print()、sys.stdout、sys.stderr的一些理解
print() 方法的语法: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) 其中file = sys.stdout的 ...
- python sys.sdout.write 和print 区别
sys.sdout.write 标准输入相当于“%value%”,输出内容没有空格,而print输出带有空格,举个例子 用sys.sdout.write: import sys for i in ra ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
随机推荐
- String.valueOf(null)
public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); ...
- Unity事件系统EventSystem简析
相关组件和类 EventSystem 1.负责InputModule的切换(因为现在游戏大部分都只有一个StanaloneInputModule,所以切换这部分可以先不考虑). 2.负责InputMo ...
- 关于爬虫的日常复习(17)——scrapy系列2
- 调试 ambari-server 总结
刚开始debug ambari-server的时候,很多逻辑都是第一次接触.其中有很多知识点还是记录一下的好,做个备忘.这些知识点对于自定义api的开发还是很有作用的. 1. api的子href的最后 ...
- (转) exp1-1:// 一次有趣的XSS漏洞挖掘分析(1)
from http://www.cnblogs.com/hookjoy/p/3503786.html 一次有趣的XSS漏洞挖掘分析(1) 最近认识了个新朋友,天天找我搞XSS.搞了三天,感觉这一套 ...
- kivy file import
from kivy.app import Appfrom kivy.uix.boxlayout import BoxLayoutfrom kivy.properties import ObjectPr ...
- html中如何清除浮动
在html中,浮动可以说是比较常用的.在页面的布局中他有着很大的作用,但是浮动中存在的问题也是比较多的.现在我们简单说一下怎么去除浮动 首先我们先简单的看一下浮动: 首先我们先创建一个简单的div,并 ...
- 求LCM(a,b)=n的(a,b)的总对数(a<=b)
\(a={p_1} ^ {a_1} *{p_1} ^ {a_1} *..........*{p_n} ^ {a_n}\) \(b={p_1} ^ {b_1} *{p_1} ^ {b_1} *..... ...
- sql 映射文件
...
- Windows 10搭建Apache2.4 + PHP7 + MySQL环境
一.准备 1.资源 Apache https://www.apachelounge.com/download/ PHP http://windows.php.net/download#php-7.0 ...