Python论坛  - 讨论区

标题:[python-chinese] 怎么判断一个文件读到了结尾,谢谢

2006年11月09日 星期四 11:14

lu_zi_2000 lu_zi_2000在163.com
星期四 十一月 9 11:14:39 HKT 2006





lu_zi_2000
2006-11-09
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061109/02066f5c/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月09日 星期四 11:25

武会超 whuichao在gmail.com
星期四 十一月 9 11:25:49 HKT 2006

在 06-11-9,lu_zi_2000<lu_zi_2000在163.com> 写道:
>
>
>
> ________________________________
>
> lu_zi_2000
> 2006-11-09
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
line=f.readline()
    if len(line)==0

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月09日 星期四 11:28

lu_zi_2000 lu_zi_2000在163.com
星期四 十一月 9 11:28:24 HKT 2006

°¦£¬ÎÒµÄ×÷·¨ºÍÄãÒ»Ñù£¬²»¹ý¾õµÃͦ²»Ë¬£¬¡¡fileÀïû¸öº¯ÊýÖ±½ÓÄÜÅжÏÂð£¿




lu_zi_2000
2006-11-09



·¢¼þÈË£º Îä»á³¬
·¢ËÍʱ¼ä£º 2006-11-09 11:26:08
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³­ËÍ£º 
Ö÷Ì⣺ Re: [python-chinese]ÔõôÅжÏÒ»¸öÎļþ¶Áµ½Á˽áβ£¬Ð»Ð»

ÔÚ 06-11-9£¬lu_zi_2000 <lu_zi_2000在163.com > дµÀ£º
>
>
>
> ________________________________
>
> lu_zi_2000
> 2006-11-09
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
line=f.readline()
    if len(line)==0
_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061109/9a3c2768/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月09日 星期四 11:44

Leo Jay python.leojay在gmail.com
星期四 十一月 9 11:44:31 HKT 2006

¿ÉÒÔÅжϰ¡¡£

СÎļþ¿ÉÒÔÒ»´ÎÐÔ¶Á³öÀ´f.read()£¬

´óÎļþÈç¹ûÊÇÒ»ÐÐÒ»Ðд¦ÀíµÄ»°¿ÉÒÔ£º
for line in file(r'c:\test'):
    ...

´óÎļþ²»ÄÜÒ»ÐÐÒ»Ðд¦ÀíµÄ»°¿ÉÒÔ£º
while True:
    l = f.read(1024)
    ...
    if len(l) < 1024: break

-- 
Best Regards,
Leo Jay
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061109/98a6bd87/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月09日 星期四 14:55

尹祥龙 yinxianglong在gmail.com
星期四 十一月 9 14:55:03 HKT 2006

line = f.readline()
if line == '':
    break


On 11/9/06, Leo Jay <python.leojay at gmail.com> wrote:
>
> 可以判断啊。
>
> 小文件可以一次性读出来f.read(),
>
> 大文件如果是一行一行处理的话可以:
> for line in file(r'c:\test'):
>     ...
>
> 大文件不能一行一行处理的话可以:
> while True:
>     l = f.read(1024)
>     ...
>     if len(l) < 1024: break
>
> --
> Best Regards,
> Leo Jay
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20061109/aaa90081/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月12日 星期日 21:58

david tdavid77在tom.com
星期日 十一月 12 21:58:52 HKT 2006

lu_zi_2000 写道:
>  
>  
> ------------------------------------------------------------------------
> lu_zi_2000
> 2006-11-09
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
line = f.readline()
while line:
    do something


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月14日 星期二 10:26

Brightman fenyon在126.com
星期二 十一月 14 10:26:14 HKT 2006

line = f.readline()
while line:
    do something

Èç¹ûÎļþÖмäÓÐһЩ¿ÕÐУ¬Ôõô¸ã£¿




Brightman
2006-11-14



·¢¼þÈË£º david
·¢ËÍʱ¼ä£º 2006-11-12 21:59:05
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³­ËÍ£º 
Ö÷Ì⣺ Re: [python-chinese]ÔõôÅжÏÒ»¸öÎļþ¶Áµ½Á˽áβ£¬Ð»Ð»

lu_zi_2000 дµÀ:
>  
>  
> ------------------------------------------------------------------------
> lu_zi_2000
> 2006-11-09
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
line = f.readline()
while line:
    do something

_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061114/eb5f7e74/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月14日 星期二 12:06

武会超 whuichao在gmail.com
星期二 十一月 14 12:06:06 HKT 2006

把位置定位到文件末尾
f.seek(0,2)
line=f.readline()
if  line:

在 06-11-14,Brightman<fenyon在126.com> 写道:
>
> line = f.readline()
> while line:
>     do something
>
> 如果文件中间有一些空行,怎么搞?
>
> ________________________________
>
> Brightman
> 2006-11-14
> ________________________________
>
> 发件人: david
> 发送时间: 2006-11-12 21:59:05
> 收件人: python-chinese在lists.python.cn
> 抄送:
> 主题: Re: [python-chinese]怎么判断一个文件读到了结尾,谢谢
>
>
> lu_zi_2000 写道:
> >
> >
> >
> ------------------------------------------------------------------------
> > lu_zi_2000
> > 2006-11-09
> >
> >
> >
> ------------------------------------------------------------------------
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> line = f.readline()
> while line:
>     do something
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月14日 星期二 15:50

麦田守望者 qcxhome在gmail.com
星期二 十一月 14 15:50:58 HKT 2006

readline()的返回值中包括了行结束字符\n或者\r\n

遇到文件尾,会返回空字符串对象。


-- 
GoogleTalk: qcxhome at gmail.com
MSN: qcxhome at hotmail.com
My Space: tkdchen.spaces.live.com
BOINC: boinc.berkeley.edu
中国分布式计算总站: www.equn.com

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号