Python学习笔记七(File&Open)
2008-09-08 20:05
489 查看
这里主要想说的是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的方式来使用。理论上两者应该没有本质的区别
一般我们都知道操作文件的基本根式是
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学习笔记015——文件file的常规操作(二进制文件)
- python学习笔记——多进程中共享内存Value & Array
- Python 爬虫学习笔记三:多页内容爬取&内容分析及格式化
- 【Python学习笔记】6.字典&集合
- 学习笔记:openwrt listen & send 方式实现进程间通信源码
- python学习笔记 loop&&raw_input 7&& if
- Androidx学习笔记(15)-- 数据存储之openFileOutput()
- python基础学习笔记<进阶>
- Solution for link error:Cannot Open File 'python27_d.lib'
- Python & Django 学习笔记
- <<Python基础教程>>学习笔记 | 第04章 | 字典
- 4.20学习笔记c# OpenFileDialog
- 小熊arm学习总结:iar7.3建立工程报错:cannot open the source file "XXXxxx.h
- <<Python基础教程>>学习笔记 | 第11章 | 文件和素材
- Python学习笔记3 print&input与变量和运算符
- 「学习笔记——Python」Google's Python Class 学习笔记
- python基础学习笔记<内建模块与第三方模块>
- <Test-Driven Development with Python>学习笔记 第一部分 测试驱动开发基础
- python---学习笔记之open()函数
- Python3学习笔记4-函数,全局变量&局部变量,内置函数