[Python]print vs sys.stdout.write
之前只是在项目中看到过,没怎么注意,正好跟对象一起看python学习手册,看到了这个部分于是来研究下。
def print(stream): """ print(value, ..., sep=' ', end='\\n', file=sys.stdout) 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. """ pass
In [2]: print ('a', 'b', sep='|')
File "<ipython-input-2-bcb798285c07>", line 1
print ('a', 'b', sep='|')
^
SyntaxError: invalid syntax
In [6]: print('a', 'b')
a b
In [7]: print('a', 'b', sep='--') #print 多个values 不用逗号分隔
a--b
In [8]: print('nihao');print('orangle')
nihao
orangle
In [9]: print('nihao', end='');print('orangle') #print 不换行
nihaoorangle
In [11]: f = open('test.txt', 'w')
In [12]: print('haha.....csdn', file=f)
In [15]: ls test.txt
驱动器 D 中的卷没有标签。
卷的序列号是 0002-FA2E
D:\code\python 的目录
2015/01/20 周二 10:37 0 test.txt
1 个文件 0 字节
0 个目录 61,124,526,080 可用字节
In [16]: f.close()
sys.stdout.write
def write(self, str):
""" write(str) -> None. Write string str to file.
Note that due to buffering, flush() or close() may be needed before
the file on disk reflects the data written. """
return ""
关系
区别
In [3]: class A(): ...: def __str__(self): ...: return "A" ...: In [5]: a = A() In [6]: print a A In [9]: import sys In [10]: sys.stdout.write(a) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-0697f962911e> in <module>() ----> 1 sys.stdout.write(a) TypeError: expected a character buffer object In [11]: sys.stdout.write(str(a)) A
运用
import sys
temp = sys.stdout #store original stdout object for later
sys.stdout = open('log.txt','w') #redirect all prints to this log file
print("testing123") #nothing appears at interactive prompt
print("another line") #again nothing appears. It is instead written to log file
sys.stdout.close() #ordinary file object
sys.stdout = temp #restore print commands to interactive prompt
print("back to normal") #this shows up in the interactive prompt
testing123 another line
sys.stdout.write 来封装日志写入
本文出自 “orangleliu笔记本”博客,转载请务必保留此出处http://blog.csdn.net/orangleliu/article/details/42915501
作者orangleliu 采用署名-非商业性使用-相同方式共享协议
[Python]print vs sys.stdout.write的更多相关文章
- PyQt(Python+Qt)学习随笔:print标准输出sys.stdout以及stderr重定向QTextBrowser等图形界面对象
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 <在Python实现print标准输出sys.stdout.st ...
- sys.stdout.write和print和sys.stdout.flush
1. 先看下官方文档 """ sys.stdout.write(string) Write string to stream. Returns the number of ...
- 在Python实现print标准输出sys.stdout、stderr重定向及捕获的简单办法
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 Python中的标准输出和错误输出由sys模块的stdout.stde ...
- print和sys.stdout
print print语句执行的操作是一个写操作,把我们从外设输入的数据写到了stdout流,并进行了一些特定的格式化.和文件方法不同,在执行打印操作是,不需要将对象转换为字符串(print已经帮我们 ...
- python 标准输入输出sys.stdout. sys.stdin
import sys, time ## print('please enter your name:')# user_input=sys.stdin.readline()# print(user_in ...
- 关于print()、sys.stdout、sys.stderr的一些理解
print() 方法的语法: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) 其中file = sys.stdout的 ...
- Python之print(args)与sys.stdout.write(string)使用总结
一.sys.stdout.write(string) import sys; # sys.stdout.write(): # 1.默认不换行 # 2.参数必须是字符串 # demo 01 x = &q ...
- 【python】print · sys.stdout · sys.stderr
参考文档 Python重定向标准输入.标准输出和标准错误 http://blog.csdn.net/lanbing510/article/details/8487997 python重定向sys.st ...
- 【Python】【Head First Python】【chapter1】2 - sys.stdout 和 print 的区别
sys.stdout 和 print 的区别 首先,通过 help(print) 得到print内建函数的参数 Help on built-in function print in module bu ...
随机推荐
- 原生nodejs在线聊天系统
前端自动化由来已久,最近为了编写自己的自动化工具,本人开始详细学习node,为了检验学习成果,决定编写一个类似于webqq的聊天系统.以下是该系统具有的模块. 登录模块(自动登录) 聊天模块(私聊,群 ...
- JavaScript之Promise对象
含义 Promise 是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6 将其写进了语言标准,统一了用法,原生提供了 Promise 对象. ...
- [SDOI 2009]HH的项链
Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝 壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此,他的项链变得 ...
- ●HDU 6021 MG loves string
题链: http://acm.hdu.edu.cn/showproblem.php?pid=6021 题解: 题意:对于一个长度为 N的由小写英文字母构成的随机字符串,当它进行一次变换,所有字符 i ...
- getopt_long函数使用【转】
转自:https://blog.csdn.net/cashey1991/article/details/7942809 平时在写程序时常常需要对命令行参数进行处理,当命令行参数个数较多时,如果按照顺序 ...
- 两个App之间调起通信
前言 经常使用一些app的分享功能,比如点击QQ分享,就从app打开(跳转到)QQ,然后分享完之后又回到我们的app,那么这是怎样实现的呢? 假设有这么一个需求,由app1跳转到app2,当app2完 ...
- text-size-adjust: none并没有什么用
在样式文件中添加text-size-adjust: none,并没有什么用,移动端浏览器自动调整字体大小还是在进行,webkit已取消对其支持,移动端避免浏览器因为横屏.竖屏模式自动缩放字体大小可以加 ...
- java.lang.SecurityException: Prohibited package name:
Prohibited package name:禁止使用的包名! 改个包名即可,全是这种bug多好啊.
- My Stuck in C++
My Stuck in C++ Zhong-Liang Xiang Oct. 1st, 2017 这个专题记录了对于我而言, c++迷一样的东西.
- random 模块
import stringprint (random.random()) # 0-1之间选浮点数print (random.randint(0,99,))#0-99之间选任意整数print (rand ...