您的位置:首页 > 编程语言 > Python开发

正确理解Python文件读写模式字w+、a+和r+

2014-04-23 17:35 609 查看
w+ 和 r+的区别不难理解,还有a+

+同时读写,即可读又可写,边写边读、边读边写,不用flush,用seek 和 tell可测得。
fp = open("a.txt", "a+", 0)
print 'open',fp.tell()
x = fp.read()
print 'open read()',fp.tell()
print x
fp.write("123456\n")
print 'write 1-6',fp.tell()
x = fp.read()
print "first read\n",x
fp.seek(0)
x = fp.read()
print "second read\n", x,fp.tell()
fp.close()
第一次运行时的结果是:
open 0
open read() 0

write 1-6 8
first read

second read
123456
8
第二次运行时的结果是
open 0
open read() 8
123456

write 1-6 16
first read

second read
123456
123456
16
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息