Size: 3834
Comment:
|
Size: 5287
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 81: | Line 81: |
你可以直接在'''open'''函数获得的文件对象上调用文件的方法比如read(象第一个程序片断中).当方法调用结束时,你不再拥有被打开文件对象的引用。 实际上,Python 检测到这个文件对象没有被引用,会马上关闭这个文件对象。然而,更佳的做法是持有一个open返回的文件对象的引用,这样当结束处理文件时,可以自己显式关闭文件。这可以保证文件被打开的时间尽可能的短,即使在Jython和以后的Python版本中由于拥有更先进的垃圾回收机制可能延缓Python中的自动关闭文件功能时,自己关闭文件也是有效的。 ... | 你可以直接在'''open'''函数获得的文件对象上调用文件的方法比如read(象第一个程序片断中).当方法调用结束时,你不再拥有被打开文件对象的引用。 实际上,Python 检测到这个文件对象没有被'''引用''',会马上关闭这个文件对象。然而,更佳的做法是持有一个open返回的文件对象的引用,这样当结束处理文件时,可以自己'''显式关闭'''文件。这可以保证文件被打开的时间尽可能的短,即使在Jython和以后的Python版本中由于拥有更先进的垃圾回收机制可能延缓Python中的自动关闭文件功能时,自己关闭文件也是有效的。 如果不是需要读取全部文件内容,而是每次读取文件的'''部分内容''',习惯方法是不同的。 下面的程序片断显示了如何从文件中每次读取100字节,一直到文件末尾: {{{ file_object = open('abinfile', 'rb') while 1: chunk = file_object.read(100) if not chunk: break do_something_with(chunk) file_object.close( ) }}} read方法中使用参数N,保证read只返回N 字节(如果接近文件末尾,可能更少)。当到达文件末尾时,read返回一个 '''空'''字符串. Reading a text file one line at a time is a frequent task. In Python 2.2 and later, this is the easiest, clearest, and fastest approach: for line in open('thefile.txt'): do_something_with(line) Several idioms were common in older versions of Python. The one idiom you can be sure will work even on extremely old versions of Python, such as 1.5.2, is quite similar to the idiom for reading a binary file a chunk at a time: file_object = open('thefile.txt') while 1: line = file_object.readline( ) if not line: break do_something_with(line) file_object.close( ) readline, like read, returns the empty string when it reaches the end of the file. Note that the end of the file is easily distinguished from an empty line because the latter is returned by readline as '\n', which is not an empty string but rather a string with a length of 1. |
文章来自《Python cookbook》. 翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!
-- [email protected] [DateTime(2004-09-19T22:05:29Z)] TableOfContents
描述
读取文件内容
Credit: Luther Blissett
问题
从文件读取文本或数据
解决
一次将文件内容读入一个长字符串的最简便方法
all_the_text = open('thefile.txt').read( ) # 文本文件的全部文本 all_the_data = open('abinfile', 'rb').read( ) # 2进制文件的全部数据
更好的方法是将文件对象和一个变量绑定,可以及时关闭文件。比如,读取文本文件内容:
file_object = open('thefile.txt') # 打开文件 all_the_text = file_object.read( ) # 文本文件的全部文本 file_object.close( ) # 使用完毕,关闭文件
将文本文件的全部内容按照分行 作为一个tuple读出有4种方法:
list_of_all_the_lines = file_object.readlines( ) # 方法 1 list_of_all_the_lines = file_object.read( ).splitlines(1) # 方法 2 list_of_all_the_lines = file_object.read().splitlines( ) # 方法 3 list_of_all_the_lines = file_object.read( ).split('\n') # 方法 4
方法1、2 返回的list中包含的string 元素末尾有'\n', 方法3、4 返回的list中包含的string元素末尾去掉了‘\n’.
方法1是效率最高的,而且最符合python的风格。
在Python 2.2以及更高版本中,有方法5和方法1等效
list_of_all_the_lines = list(file_object) # 方法 5
讨论
如果文件大小不是特别大,那么一次将文件内容全部读出是最快的,一般来说对于以后文件内容的处理也是最方便的。
内置函数 open,打开文件并返回一个文件对象实例。 可以调用read方法读取文件内容(文本或2进制)到一个大字符串。
如果是文本内容,可以使用string的split方法或者特别的splitlines方法将此string分解成文本行的list。
经常进行文件内容按行分解,可以直接调用文件对象的readlines方法,更方便更快捷。:)。
在Python 2.2以及更高版本中,可以直接将文件对象实例作为内置list函数的唯一参数。
在Unix 以及类Unix系统,比如Linux和BSD的各种变体中,文本文件和2进制文件没有实质的区别。
在Windows和Macintosh系统中,文本文件中的分行符不是标准的'\n'分隔符,分别是'\r\n'和'\r'。
打开文本文件时,Python会自动用'\n'替换相应的分隔符。
因此, 打开2进制文件不需要这种自动替换时,必须通知Python:
open打开文件时第2个参数使用rb。
在类Unix平台上使用rb参数也没有什么坏处。 虽然不是必须的,但是区分文本文件和2进制文件是一个好习惯,可以使程序易于理解,同时具有较好的跨平台性。
你可以直接在open函数获得的文件对象上调用文件的方法比如read(象第一个程序片断中).当方法调用结束时,你不再拥有被打开文件对象的引用。 实际上,Python 检测到这个文件对象没有被引用,会马上关闭这个文件对象。然而,更佳的做法是持有一个open返回的文件对象的引用,这样当结束处理文件时,可以自己显式关闭文件。这可以保证文件被打开的时间尽可能的短,即使在Jython和以后的Python版本中由于拥有更先进的垃圾回收机制可能延缓Python中的自动关闭文件功能时,自己关闭文件也是有效的。
如果不是需要读取全部文件内容,而是每次读取文件的部分内容,习惯方法是不同的。
下面的程序片断显示了如何从文件中每次读取100字节,一直到文件末尾:
file_object = open('abinfile', 'rb') while 1: chunk = file_object.read(100) if not chunk: break do_something_with(chunk) file_object.close( )
read方法中使用参数N,保证read只返回N 字节(如果接近文件末尾,可能更少)。当到达文件末尾时,read返回一个 空字符串.
Reading a text file one line at a time is a frequent task. In Python 2.2 and later, this is the easiest, clearest, and fastest approach:
for line in open('thefile.txt'):
- do_something_with(line)
Several idioms were common in older versions of Python. The one idiom you can be sure will work even on extremely old versions of Python, such as 1.5.2, is quite similar to the idiom for reading a binary file a chunk at a time:
file_object = open('thefile.txt') while 1:
- line = file_object.readline( ) if not line: break do_something_with(line)
file_object.close( ) readline, like read, returns the empty string when it reaches the end of the file. Note that the end of the file is easily distinguished from an empty line because the latter is returned by readline as '\n', which is not an empty string but rather a string with a length of 1.