python中file和open_Python中的file和open简述
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)
in ()
----> 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)
in ()
----> 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_Python中的file和open简述相关推荐
- python中打开文件open_Python中打开文件的方式(With open)
1.读文件 要以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标 示符: f = open( '/Users/michael/test.txt', 'r' ) 标示 ...
- python中出现[Errno 2] No such file or directory解决方法之一
在我学习python文件部分的过程中 编写如下代码时: 会出现跳出如下错误 而我在该目录下的确有此文件 我查了很多资料都还是没有解决,最后我斥巨资(15¥)才得以解决,在这里我分享给大家 其实在这里主 ...
- java中file类_Java中file类
File类的概述:File更应该叫做一个路径,文件路径或者文件夹路径 ,路径分为绝对路径和相对路径,绝对路径是一个固定的路径,从盘符开始,相对路径相对于某个位置,在eclipse下是指当前项目下,在d ...
- zip: stdin: unexpected end of file tar: 归档文件中异常的 EOF tar: 归档文件中异常的 EOF tar: Error is not recoverable
在linux解压报错 zip: stdin: unexpected end of file tar: 归档文件中异常的 EOF tar: 归档文件中异常的 EOF tar: Error is not ...
- ROS(Robot Operating System)笔记 : 1.使用launch file在gazebo中生成urdf机器人
ROS(Robot Operating System) 1.使用launch file在gazebo中生成urdf机器人 最近接触了ROS(Robot Operating System),发现单单学习 ...
- android+图标+i_explore+无背景,Android Studio中Android Device Monitor中的File Explore不显示文...
环境:操作系统是Mac,模拟器 问题:Android Studio中Android Device Monitor中的File Explore不显示文件 本人在自学文件存储,想查看"dada/ ...
- php 时间戳存储 原因,将php文件中的unix时间戳存储到mysql中(store unix timestamp from php file into mysql)...
将php文件中的unix时间戳存储到mysql中(store unix timestamp from php file into mysql) 现在我有这个代码: $mysqldate = date( ...
- file结构体中private_data指针的疑惑
转:http://www.360doc.com/content/12/0506/19/1299815_209093142.shtml hi all and barry, 最近在学习字符设备驱动,不太明 ...
- java file 方法_JAVA中File的常用方法
一.File类中包含了很多获得文件或文件夹属性的方法,使用起来比较方便,下面将常见的方法介绍如下: a.createNewFile方法 public boolean createNewFile() t ...
最新文章
- git fetch -p 获取远程仓库的新分支以及删除远程仓库已删除的分支
- CentOS 7 修改终端字符界面分辨率
- CLR探索系列:Windbg+SOS动态调试分析托管代码
- Java HashSet源码解析
- C++——构造和析构函数
- oracle sum(col1) over(partition by col2 order by col3):实现分组递增汇总
- Node.js + Express + handlebars搭建个人网站(1)
- C#操作Access数据库 增删改查
- soa学习路线_Web服务安全性和SOA路线图的人为维度
- Cibersort免疫浸润的在线分析及R语言代码实现
- 【月报】Java知音的二月汇总
- Git之同一台电脑如何连接多个远程仓库
- [数据库]特殊符号对应的ascii和键盘字符的十六进制表示
- Python(六):dict、set
- 农场游戏开发记录十二
- 中国联通沃支付echop支付插件
- 快递公司面单纸张标准
- Playbook特殊用法
- Misc训练笔记(一)
- 应对羊毛党的老手段不管用了,但有些公司依然有办法,他们是怎么做的?