不能在文件中间写?

100 views
Skip to first unread message

Jack phil

unread,
Sep 29, 2012, 3:31:40 AM9/29/12
to pyth...@googlegroups.com
为什么我运行如下代码后只有line1,2,3,去掉注释就有4行,注释掉f.readline(),则只有line4,2,3

import os
with open('test.txt', 'w+') as f:
    f.write("this is line1\n")
    f.write("this is line2\n")
    f.write("this is line3\n")
    f.seek(0)
    f.readline()
    #f.seek(0, os.SEEK_END)
    f.write("this is line4")


kimjxie

unread,
Sep 29, 2012, 4:27:25 AM9/29/12
to pyth...@googlegroups.com
read write都会改变文件指针位置
下一次的read write操作就在上一次位置进行

2012/9/29 Jack phil <jack...@gmail.com>


--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
退订: python-cn+...@googlegroups.com (向此发空信即退!)
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp

limodou

unread,
Sep 29, 2012, 4:57:39 AM9/29/12
to pyth...@googlegroups.com


2012/9/29 Jack phil <jack...@gmail.com>
文件是随机存储的,不是数据库。所以当你指针回去后再写会替换掉以前的内容。不能起到插入的效果。 

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou

Jack phil

unread,
Sep 29, 2012, 9:24:33 PM9/29/12
to pyth...@googlegroups.com
我也是这么理解的,关键是没有line4出现啊,如果“就在上一次位置”开始写,应该会覆盖后面的内容,就有line4出现啊

依云

unread,
Sep 29, 2012, 9:34:13 PM9/29/12
to pyth...@googlegroups.com
我这里的确有四行,但最后一行没有回车符。你是不是在 bash 下用 cat 查看,
然后最后一行在提示符前你没注意到?

On Sun, Sep 30, 2012 at 09:24:33AM +0800, Jack phil wrote:
> 我也是这么理解的,关键是没有line4出现啊,如果“就在上一次位置”开始写,应该会覆盖后面的内容,就有line4出现啊
>
> 在 2012年9月29日 下午4:27,kimjxie <kim...@gmail.com>写道:
>
> > read write都会改变文件指针位置
> > 下一次的read write操作就在上一次位置进行
> >
> > 2012/9/29 Jack phil <jack...@gmail.com>
> >
> >> 为什么我运行如下代码后只有line1,2,3,去掉注释就有4行,注释掉f.readline(),则只有line4,2,3
> >>
> >> import os
> >> with open('test.txt', 'w+') as f:
> >> f.write("this is line1\n")
> >> f.write("this is line2\n")
> >> f.write("this is line3\n")
> >> f.seek(0)
> >> f.readline()
> >> #f.seek(0, os.SEEK_END)
> >> f.write("this is line4")

--
Best regards,
lilydjwg

Linux Vim Python 我的博客:
http://lilydjwg.is-programmer.com/
--
A: Because it obfuscates the reading.
Q: Why is top posting so bad?

Jack phil

unread,
Oct 1, 2012, 2:51:39 AM10/1/12
to pyth...@googlegroups.com
不是,我用的是python 2.7.2 64位windows版,系统是win7 64位旗舰版。刚才又试了一下,只有3行(不包括最后的换行,也就是第4行是空行),难道是bug?

Jack phil

unread,
Oct 1, 2012, 3:06:00 AM10/1/12
to pyth...@googlegroups.com
第二行Linux下显示:this is line4 ,这个符合预期

win7上显示:this is line2 ,这是不理解的地方

依云

unread,
Oct 1, 2012, 4:22:12 AM10/1/12
to pyth...@googlegroups.com
我测试的结果:

WinXP + Python 2.7, 三行,123
WinXP + Python 3.2,四行,1234
WinXP + ANSI C,三行,123
Linux + any,三行,143

依云

unread,
Oct 1, 2012, 4:24:02 AM10/1/12
to pyth...@googlegroups.com
MSDN 的说明:

However, when you switch between reading and writing, there must be an
intervening fflush, fsetpos, fseek, or rewind operation. The current
position can be specified for the fsetpos or fseek operation, if
desired.

不知道 Python 3.2 是怎么搞的。

依云

unread,
Oct 1, 2012, 5:01:35 AM10/1/12
to pyth...@googlegroups.com
这个弄错了,Linux + Python 3.2 和 WinXP 下是一样的。

On Mon, Oct 01, 2012 at 04:22:11PM +0800, 依云 wrote:

Jack phil

unread,
Oct 1, 2012, 5:13:24 AM10/1/12
to pyth...@googlegroups.com
谢谢,看来定位还是用seek

Reply all
Reply to author
Forward
0 new messages