免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7160 | 回复: 6
打印 上一主题 下一主题

Python学习笔记(File()和Open()) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-09 18:25 |只看该作者 |倒序浏览
很基础的东西,但刚学的时候,我是很迷惑。到底操作文件用那个方法。


看看python的介绍:
两种都是build-in function
file( filename[, mode[, bufsize]])
Constructor function for the file type, described further in section 3.9, ``File Objects''. The constructor's arguments are the same as those of the open() built-in function described below.
When opening a file, it's preferable to use open() instead of invoking this constructor directly. file is more suited to type testing (for example, writing "isinstance(f, file)").
open( filename[, mode[, bufsize]])
Open a file, returning an object of the file type described in section 3.9, ``File Objects''. If the file cannot be opened, IOError is raised. When opening a file, it's preferable to use open() instead of invoking the file constructor directly.....

所以这么来解释
file是file类型的构造函数,虽然也可以打开文件,但更提倡使用open的方式来使用。两者应该没有本质的区别 ,所以大家打开文件还是用open(),应该规范一些。

我现在就是这样理解的。不知道是否正确

[ 本帖最后由 pumaboyd 于 2008-9-9 18:26 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-09-09 19:36 |只看该作者
有区别吧,一个是类构造,一个是内建函数,虽然open的实现可能就和下面的差不多
def open(filename, mode='r'):
    return file(filename, mode)

它说地很清楚了,推荐使用open

[ 本帖最后由 xiaonanln 于 2008-9-9 19:37 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-09-10 09:19 |只看该作者
建议楼主把学习笔记集中在一个帖子里。

呵呵。

论坛徽章:
3
CU大牛徽章
日期:2013-03-13 15:29:07CU大牛徽章
日期:2013-03-13 15:29:49CU大牛徽章
日期:2013-03-13 15:30:19
4 [报告]
发表于 2008-09-11 08:56 |只看该作者
俺一般都用open( filename[, mode[, bufsize]])

论坛徽章:
0
5 [报告]
发表于 2008-09-11 11:33 |只看该作者

学习最重要是坚持,

偶每次记了一点就放弃了,,

    可惜了啊

论坛徽章:
0
6 [报告]
发表于 2008-09-11 16:27 |只看该作者
也在学习中。。。。。。。。。

论坛徽章:
0
7 [报告]
发表于 2008-09-12 04:18 |只看该作者
回#2,恩好的,尽量集中。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP