[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 ...
随机推荐
- Pycharm数据库连接错误
简单地总结就一句话: Python2的mysql库为:mysqldb,而Python3的为:pymysql. 当我们使用Pycharm开发项目时,首先需要下载安装相对应的数据库,以及在项目根目录下的s ...
- [Noi2016]区间
题目描述 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含至少一个位置.换句话说,就是使得存在一个 x,使得对于每一 ...
- WISCO信息组NOIP模拟赛-数据结构
传送门 差分+暴力 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstri ...
- hdu 3436 splay树+离散化*
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- IPQ4028开启I2C功能
0 概述 IPQ4028是一款集约式4核心ARM7 SOC芯片,内嵌独立双频WiFi子系统,offload式,支持MU-MIMO,最高支持1.2Gbps.标准的官方Demo方案中,IPQ4019开启了 ...
- RESTful 最佳实战
在GitHub上看到一本不错的关于REST实战的书,很高兴分享阅读笔记.[下载地址] 一.什么是REST(WHAT) REST架构描述了六种约束.(统一接口.无状态.可缓存.CS架构.分层系统.按需编 ...
- TortiseGit 添加SSH-Key
TortoiseGit 使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥.使用命令ssh-keygen -C "邮箱地址" -t rsa产生的密钥在Tortoi ...
- 41. First Missing Positive(困难, 用到 counting sort 方法)
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- SUSE11虚拟机安装与Oracle 11g安装
SUSE11虚拟机安装与Oracle 11g安装 本文中所需所有参数均位于文末附录中 新建虚拟机,选择SUSE11 64位 启动虚拟机后,选择第二项安装 选择语言 跳过CD检查 选择全新安装 选择默认 ...
- 工作流程,编程,调试,性能:Unity游戏开发者应该学习的20个改进技巧
Unity 是一个备受欢迎的游戏开发平台.它的功能令人印象深刻,同时也迎合了不同的游戏开发需求.游戏开发者可以使用 Unity 创建任何类型的游戏,从世界级的 RPG 游戏到最流行的增强现实游戏 Po ...