赞
踩
open函数原型
open(name[, mode[, buffering]])
关于第三个参数buffering,文档中的解释是
The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size. A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used.
可以看到:
若buffering为0或False,没有缓冲区;
f = open(r’D:\code\py\opentest’, ‘w’, False),
f.write(‘望水至极’)
去opentest文件查看,字符串已被写入文件。
若为1或True有缓冲区;
f = open(r’D:\code\py\opentest’, ‘w’, True)
f.write(‘望水至极’)
有缓冲区,不过文档时说“1 means line buffered”行缓冲?不理解什么意思,不过使用上感觉和使用默认缓冲区一样;
若缓冲区满了则自动写入文件opentest中,否则需要f.flush()或f.close(),才能写入ope
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。