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的方式来使用。理论上两者应该没有本质的区别 优质内容筛选与推荐>>
1、Git Step by Step – (6) Git远程仓库
2、OpenCV for Ios 学习笔记(4)-标记检测1
3、面试浅谈
4、SWT中生成任务栏图标
5、globbing


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号