help(file) help(open)

老规矩先看一下内置的帮助文档怎么描述file和open,毕竟官方文档是最直接最准确的描述。

Help on class file in module __builtin__:

class file(object)
| file(name[, mode[, buffering]]) -> file object
|
| Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
| writing or appending. The file will be created if it doesn't exist
| when opened for writing or appending; it will be truncated when
| opened for writing. Add a 'b' to the mode for binary files.
| Add a '+' to the mode to allow simultaneous reading and writing.
| If the buffering argument is given, 0 means unbuffered, 1 means line
| buffered, and larger numbers specify the buffer size. The preferred way
| to open a file is with the builtin open() function.
| Add a 'U' to mode to open the file for input with universal newline
| support. Any line ending in the input file will be seen as a '\n'
| in Python. Also, a file so opened gains the attribute 'newlines';
| the value for this attribute is one of None (no newline read yet),
| '\r', '\n', '\r\n' or a tuple containing all the newline types seen.

简单来说就是file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建。但是更推荐使用内置函数open()来打开一个文件,所以我们再看一下open()的介绍:

Help on built-in function open in module __builtin__:

open(...)
open(name[, mode[, buffering]]) -> file object Open a file using the file() type, returns a file object. This is the
preferred way to open a file. See file.__doc__ for further information.
(END)

首先open是内置函数,使用方式是open('file_name', mode, buffering),返回值也是一个file对象,同样,以写模式打开文件如果不存在也会被创建一个新的。

使用实例

In [8]: f1 = open('test.py')                                                                                                                                                                      

In [9]: f1.
f1.close f1.fileno f1.name f1.readinto f1.softspace f1.writelines
f1.closed f1.flush f1.newlines f1.readline f1.tell f1.xreadlines
f1.encoding f1.isatty f1.next f1.readlines f1.truncate
f1.errors f1.mode f1.read f1.seek f1.write In [9]: f1.rea
f1.read f1.readinto f1.readline f1.readlines In [9]: f1.readli
f1.readline f1.readlines In [9]: f1.readlines()
Out[9]:
['import logging\n',
'\n',
"logging.basicConfig(filename='test.log', level=logging.INFO)\n",
"logging.info('Started')\n",
"print 'x' + 1\n",
"logging.info('Finished')\n"] In [10]: f1.cl
f1.close f1.closed In [10]: f1.close() In [11]: f2 = file('test.py') In [12]: f2.
f2.close f2.fileno f2.name f2.readinto f2.softspace f2.writelines
f2.closed f2.flush f2.newlines f2.readline f2.tell f2.xreadlines
f2.encoding f2.isatty f2.next f2.readlines f2.truncate
f2.errors f2.mode f2.read f2.seek f2.write In [12]: f2.read
f2.read f2.readinto f2.readline f2.readlines In [12]: f2.readli
f2.readline f2.readlines In [12]: f2.readlines()
Out[12]:
['import logging\n',
'\n',
"logging.basicConfig(filename='test.log', level=logging.INFO)\n",
"logging.info('Started')\n",
"print 'x' + 1\n",
"logging.info('Finished')\n"] In [13]: f2.cl
f2.close f2.closed In [13]: f2.closed()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-24c97e0e079e> in <module>()
----> 1 f2.closed() TypeError: 'bool' object is not callable In [14]: f2.closed
Out[14]: False In [15]: f2.close() # 打开不存在的文件 In [18]: f3 = file('txt.txt', 'r+')
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-18-3e9262877eea> in <module>()
----> 1 f3 = file('txt.txt', 'r+') IOError: [Errno 2] No such file or directory: 'txt.txt' In [19]: f3 = file('txt.txt', 'w+')

可以看出来使用的时候区别也不大,不过注意Py3中已经没有了file~这可能也是推荐使用open的一个很重要的原因吧

Python中的file和open简述的更多相关文章

  1. 转发 python中file和open有什么区别

    python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...

  2. python中使用openpyxl模块时报错: File is not a zip file

    python中使用openpyxl模块时报错: File is not a zip file. 最大的原因就是不是真正的 xlsx文件, 如果是通过 库xlwt  新建的文件,或者是通过自己修改后缀名 ...

  3. 简述python中`functools.wrapper()

    简述python中functools.wrapper() 首先对于最简单的函数: def a(): pass if __name__ == '__main__': print(a.__name__) ...

  4. Python中关于with open file as 的用法

    最近用到python来处理文本文件了,然后需要处理文件.发现python中提供的with   open  as   这个还是用的不错的!好的,废话不多说了,看下例子: with open('./sig ...

  5. 简述Python中的break和continue的区别

    众所周知在Python中,break是结束整个循环体,而continue则是结束本次循环再继续循环. 但是作为一个新手的你,还是不明白它们的区别,这里用一个生动的例子说明它们的区别,如下: 1.con ...

  6. python中进程、线程、协程简述

    进程 python中使用multiprocessing模块对进程进行操作管理 进程同步(锁.信号量.事件) 锁 —— multiprocessing.Lock 只要用到了锁 锁之间的代码就会变成同步的 ...

  7. [转]Python中的str与unicode处理方法

    早上被python的编码搞得抓耳挠腮,在搜资料的时候感觉这篇博文很不错,所以收藏在此. python2.x中处理中文,是一件头疼的事情.网上写这方面的文章,测次不齐,而且都会有点错误,所以在这里打算自 ...

  8. python中的TypeError错误解决办法

    新手在学习python时候,会遇到很多的坑,下面来具体说说其中一个. 在使用python编写面向对象的程序时,新手可能遇到TypeError: this constructor takes no ar ...

  9. python中的迭代、生成器等等

    本人对编程语言实在是一窍不通啊...今天看了廖雪峰老师的关于迭代,迭代器,生成器,递归等等,word天,这都什么跟什么啊... 1.关于迭代 如果给定一个list或tuple,我们可以通过for循环来 ...

随机推荐

  1. B样条曲线曲面(附代码)

    1 B样条曲线 1.1 B样条曲线方程 B样条方法具有表示与设计自由型曲线曲面的强大功能,是形状数学描述的主流方法之一,另外B样条方法是目前工业产品几何定义国际标准——有理B样条方法 (NURBS)的 ...

  2. Spring 4 集成Apache CXF开发JAX-RS Web Service

    什么是JAX-RS 在JSR-311规范中定义,即Java API for RESTful Web Services,一套Java API,用于开发 RESTful风格的Webservice. 工程概 ...

  3. js中A包含B的写法与分割字符串的方法

    在java中A包含B的写法 if(A.contains(B)){ ... } 在js中没有contains方法,应该使用下面这种方法: var an = "传染性.潜伏性.破坏性" ...

  4. MS SQL 错误 :17883,严重度: 1,状态: 0

    公司一台老旧的SQL SERVER 2000 数据库,一周内会出现若干次(一次或多次)CPU 持续100%,导致应用程序没有反应的情况,如下图所示: 错误信息如下所示: 日期 2013/7/12 2: ...

  5. tomcat 设置jvm内存

    修改 tomcat安装目录\bin\catalina.bat在set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%这行下面加上 set JAVA_OPTS=%JAVA_ ...

  6. 【转载】Markdown使用笔记

    献给写作者的 Markdown 新手指南 http://www.jianshu.com/p/q81RER 「简书」作为一款「写作软件」在诞生之初就支持了 Markdown,Markdown 是一种「电 ...

  7. 13、Apache中虚拟目录和目录权限配置

    一.虚拟目录 之前的个人主页,为了安全起见,需要把~yanji 用户隐藏起来,这时就可以设置个 虚拟目录. 它在Apache服务器应用比较多,能够隐藏系统的真实目录,实用性非常高. 虚拟目录主要 通过 ...

  8. 迅为最新推出iTOP-6818开发平台无缝支持4418开发板

    iTOP-6818开发板是一款四核ARM 八核开发板与iTOP-4418开发板完全兼容,CPU主频1.4GHz,内存1GB DDR3(2GB可选),存储16GB EMMC,板载千兆以太网,GPS,WI ...

  9. 基于.net开发chrome核心浏览器【七】

    这是一个系列的文章,前面六篇文章的地址如下: 基于.net开发chrome核心浏览器[六] 基于.net开发chrome核心浏览器[五] 基于.net开发chrome核心浏览器[四] 基于.net开发 ...

  10. js parseInt和map函数

    今天看了一个js的题目["1","2","3"].map(parseInt),看到后脑海中浮现的答案是[1,2,3],但是看到正确答案后蒙了 ...