这里主要想说的是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的方式来使用。理论上两者应该没有本质的区别
分享到:
相关推荐
Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf ...
Python 笔记源码——内含python后端&机器学习等.zip Python 笔记源码——内含python后端&机器学习等.zip Python 笔记源码——内含python后端&机器学习等.zip Python 笔记源码——内含python后端&机器学习等.zip ...
最新Python学习笔记,Day 3,总结性学习笔记,适合初学者归纳总结。
Python学习笔记
Python基础入门教程,适合Python初学者,文档内容包括, 目录 前言 i 第一章 程序 1 1.1 程序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 什么是调试 . . . . . . . . . . . . . . . ....
Python学习笔记.pdf
1、Python环境搭建 1.1 Python简介 1.2 Python语言的发展历程 1.3 Python语言的主要特点 1.4 Python应用场景 1.5 Python环境搭建 1.6 第一个Python程序 1.7 集成开发工具 2、Python语法基础 2.1 Python中的...
python学习笔记python学习笔记python学习笔记python学习笔记python学习笔记
python脚本示例,收藏&学习
Python 学习笔记
python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf ...
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
最新Python学习笔记,Day 5,总结性学习笔记,适合初学者归纳总结。
python 学习笔记资料python 学习笔记资料python 学习笔记资料python 学习笔记资料
王纯业的Python学习笔记非常适合初学者入门的Python教程
Python学习笔记--皮大庆
王纯业版《Python学习笔记》.pdf
python学习笔记+源码练习,简单易懂,让你从入门到不放弃
王纯业的Python学习笔记