设为首页 加入收藏

TOP

Python中的file和open简述(一)
2017-09-30 17:40:31 】 浏览:1980
Tags: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)
<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+')
--------------------------------------------------------------------
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[Django_1_1]第一个app 下一篇HTTPResponse object — JSON obj..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目