`
pumaboyd
  • 浏览: 242159 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Python学习笔记七(File&Open)

阅读更多

这里主要想说的是File()和Open()这两个函数的主要用途,因为自己在用的时候就迷糊,到底这俩个有什么区别
一般我们都知道操作文件的基本根式是
with open("filePath","r") as fobj
    for line in fobj
      .......
文件的操作和很多编程语言一样,一定要记得Close打开的文件对象。其中文件的打开方式也有r(读)w(截断写)a(追加方式),常用的就这几种,还有些特殊的就查帮助文档吧。这些都好理解,最让我迷惑的是File()和Open()这两个有什么用,如果是一样的,干嘛不统一呢。先看英文吧

>不多介绍先看看英文资料
两种都是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的方式来使用。理论上两者应该没有本质的区别

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics