Size: 10469
Comment:
|
Size: 15971
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
'''4.2. File Tools ''' [[BR]] ''' 4.2. 文件工具 ''' [[BR]][[BR]] External files are at the heart of much of what we do with shell utilities. For instance, a testing system may read its inputs from one file, store program results in another file, and check expected results by loading yet another file. Even user interface and Internet-oriented programs may load binary images and audio clips from files on the underlying computer. It's a core programming concept. |
'''4.2. File Tools ''' [[BR]] ''' 4.2. 文件工具 ''' [[BR]][[BR]] External files are at the heart of much of what we do with shell utilities. For instance, a testing system may read its inputs from one file, store program results in another file, and check expected results by loading yet another file. Even user interface and Internet-oriented programs may load binary images and audio clips from files on the underlying computer. It's a core programming concept. |
Line 9: | Line 5: |
[[BR]] In Python, the built-in {{{open}}} function is the primary tool scripts use to access the files on the underlying computer system. Since this function is an inherent part of the Python language, you may already be familiar with its basic workings. Technically, {{{open}}} gives direct access to the {{{stdio}}} filesystem calls in the system's C libraryit returns a new file object that is connected to the external file and has methods that map more or less directly to file calls on your machine. The {{{open}}} function also provides a portable interface to the underlying filesystemit works the same way on every platform on which Python runs. |
[[BR]] In Python, the built-in {{{open}}} function is the primary tool scripts use to access the files on the underlying computer system. Since this function is an inherent part of the Python language, you may already be familiar with its basic workings. Technically, {{{open}}} gives direct access to the {{{stdio}}} filesystem calls in the system's C libraryit returns a new file object that is connected to the external file and has methods that map more or less directly to file calls on your machine. The {{{open}}} function also provides a portable interface to the underlying filesystemit works the same way on every platform on which Python runs. |
Line 14: | Line 9: |
Other file-related interfaces in Python allow us to do things such as manipulate lower-level descriptor-based files ({{{os}}} module), store objects away in files by key ({{{anydbm}}} and {{{shelve}}} modules), and access SQL databases. Most of these are larger topics addressed in [file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-19.html#python3-CHP-19 Chapter 19]. | [[BR]] Other file-related interfaces in Python allow us to do things such as manipulate lower-level descriptor-based files ({{{os}}} module), store objects away in files by key ({{{anydbm}}} and {{{shelve}}} modules), and access SQL databases. Most of these are larger topics addressed in [[Ch-19-1]]. |
Line 16: | Line 11: |
Pyhton中其他文件相关的接口允许我们处理基于底层的基于描述符的文件({{{os}}}模块),将对象以索引的方式存储在文件中.({{{anydbm}}} and {{{shelve}}} 模块), 访问SQL数据库. 这些主题将在[file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-19.html#python3-CHP-19 19章]详细讲解. | Pyhton中其他文件相关的接口允许我们处理基于底层的基于描述符的文件({{{os}}}模块),将对象以索引的方式存储在文件中.({{{anydbm}}} and {{{shelve}}} 模块), 访问SQL数据库. 这些主题将在[[Ch-19-1]]详细讲解. |
Line 18: | Line 13: |
In this chapter, we'll take a brief tutorial look at the built-in file object and explore a handful of more advanced file-related topics. As usual, you should consult the library manual's file object entry for further details and methods we don't have space to cover here. Remember, for quick interactive help, you can also run {{{dir(file)}}} for an attributes list with methods, {{{help(file)}}} for general help, and {{{help(file.read)}}} for help on a specific method such as {{{read}}}. The built-in name {{{file}}} identifies the file datatype in recent Python releases.^[file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-4-SECT-2.html#python3-CHP-4-FN-1 [*]]^ | [[BR]] In this chapter, we'll take a brief tutorial look at the built-in file object and explore a handful of more advanced file-related topics. As usual, you should consult the library manual's file object entry for further details and methods we don't have space to cover here. Remember, for quick interactive help, you can also run {{{dir(file)}}} for an attributes list with methods, {{{help(file)}}} for general help, and {{{help(file.read)}}} for help on a specific method such as {{{read}}}. The built-in name {{{file}}} identifies the file datatype in recent Python releases.^[[*]]^ |
Line 20: | Line 15: |
本章中, 我们将通过一个简单的教程认识内建的文件对象和少量高级的文件相关主题. 同样, 你应当通过查阅对象指导文件学习更详细的信息和方法.我们这里没有太多的章节去覆盖, 注意, ,在交互中,你可以通过运行{{{dir(file)}}}来得到方法属性的列表,从而更快地得到帮助. {{{help(file)}}} 来得到基本帮助信息, 同样{{{help(file.read)}}} 可以得到某个方法的详细帮助.例如{{{read}}}.在最近的Python发行版中. 内建的{{{file}}} 用来标识file数据类型.^[file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-4-SECT-2.html#python3-CHP-4-FN-1 [*]]^ | 本章中, 我们将通过一个简单的教程认识内建的文件对象和少量高级的文件相关主题. 同样, 你应当通过查阅对象指导文件学习更详细的信息和方法.我们这里没有太多的章节去覆盖, 注意, ,在交互中,你可以通过运行{{{dir(file)}}}来得到方法属性的列表,从而更快地得到帮助. {{{help(file)}}} 来得到基本帮助信息, 同样{{{help(file.read)}}} 可以得到某个方法的详细帮助.例如{{{read}}}.在最近的Python发行版中. 内建的{{{file}}} 用来标识file数据类型.^[[*]]^ |
Line 22: | Line 17: |
^^ Technically, you can use the name {{{file}}} anywhere you use {{{open}}}, though {{{open}}} is still the generally preferred call unless you are subclassing to customize files. We'll use {{{open}}} in most of our examples. As for all built-in names, it's OK to use the name {{{file}}} for your own variables as long as you don't need direct access to the built-in file datatype (your {{{file}}} name will hide the built-in scope's {{{file}}}). In fact, this is such a common practice that we'll frequently follow it here. This is not a sin, but you should generally be careful about reusing built-in names in this way. | . ^^ Technically, you can use the name {{{file}}} anywhere you use {{{open}}}, though {{{open}}} is still the generally preferred call unless you are subclassing to customize files. We'll use {{{open}}} in most of our examples. As for all built-in names, it's OK to use the name {{{file}}} for your own variables as long as you don't need direct access to the built-in file datatype (your {{{file}}} name will hide the built-in scope's {{{file}}}). In fact, this is such a common practice that we'll frequently follow it here. This is not a sin, but you should generally be careful about reusing built-in names in this way. ^^ 技术上讲, 你可以在所有你用{{{open}}}的地方使用{{{file}}}, 因此{{{open}}}仍然是常用的调用方法除非你通用子类来定制它.我们的大部分例子使用{{{open}}}. 对于内建的名称, 只有当你不需要访问内建file数据类型时你可以将file作为你的变量(你的{{{file}}}将会覆盖内建范围的{{{file}}}). 事实上.这是一种常用的实践做法.我们应遵循它, 这并不是种罪恶,当你需要重用内建的名称时应注意. ''' 4.2.1. Built-In File Objects ''' [[BR]] ''' 4.2.1. 内建文件对象 ''' [[BR]][[BR]] For most purposes, the {{{open}}} function is all you need to remember to process files in your scripts. The file object returned by {{{open}}} has methods for reading data ({{{read}}}, {{{readline}}}, {{{readlines}}}), writing data ({{{write}}}, {{{writelines}}}), freeing system resources ({{{close}}}), moving about in the file ({{{seek}}}), forcing data to be transferred out of buffers ({{{flush}}}), fetching the underlying file handle ({{{fileno}}}), and more. Since the built-in file object is so easy to use, though, let's jump right into a few interactive examples. |
Line 24: | Line 20: |
^^ 技术上讲, 你可以在所有你用{{{open}}}的地方使用{{{file}}}, 因此{{{open}}}仍然是常用的调用方法除非你通用子类来定制它.我们的大部分例子使用{{{open}}}. 对于内建的名称, 只有当你不需要访问内建file数据类型时你可以将file作为你的变量(你的{{{file}}}将会覆盖内建范围的{{{file}}}). 事实上.这是一种常用的实践做法.我们应遵循它, 这并不是种罪恶,当你需要重用内建的名称时应注意. | 在大部分应用中, 你的脚本只需要用到{{{open}}}函数处理文件. {{{open}}}函数返回的文件对象有多种读入数据的方法({{{read}}}, {{{readline}}}, {{{readlines}}}), 写入数据({{{write}}}, {{{writelines}}}), 释放系统资源({{{close}}}), 文件内容中移动({{{seek}}}), 强制清除缓存中的数据({{{flush}}}), 取得本地文件句柄({{{fileno}}}), 以及其他功能. 文件对象的使用是相当简单的.让我们做一些的例子. |
Line 26: | Line 22: |
''' 4.2.1. Built-In File Objects ''' [[BR]] ''' 4.2.1. 内建文件对象 ''' [[BR]][[BR]] For most purposes, the {{{open}}} function is all you need to remember to process files in your scripts. The file object returned by {{{open}}} has methods for reading data ({{{read}}}, {{{readline}}}, {{{readlines}}}), writing data ({{{write}}}, {{{writelines}}}), freeing system resources ({{{close}}}), moving about in the file ({{{seek}}}), forcing data to be transferred out of buffers ({{{flush}}}), fetching the underlying file handle ({{{fileno}}}), and more. Since the built-in file object is so easy to use, though, let's jump right into a few interactive examples. 在大部分应用中, 你的脚本只需要用到{{{open}}}函数处理文件. {{{open}}}函数返回的文件对象有多种读入数据的方法({{{read}}}, {{{readline}}}, {{{readlines}}}), 写入数据({{{write}}}, {{{writelines}}}), 释放系统资源({{{close}}}), 文件内容中移动({{{seek}}}), 强制清除缓存中的数据({{{flush}}}), 取得本地文件句柄({{{fileno}}}), 以及其他功能. 文件对象的使用是相当简单的.让我们做一些的例子 ''' 4.2.1.1. Output files ''' [[BR]] ''' 4.2.1.1. 输出文件''' [[BR]][[BR]] To make a new file, call {{{open}}} with two arguments: the external name of the file to be created and a mode string {{{w}}} (short for write). To store data on the file, call the file object's {{{write}}} method with a string containing the data to store, and then call the {{{close}}} method to close the file if you wish to open it again within the same program or session: |
[[BR]] ''' 4.2.1.1. Output files ''' [[BR]] ''' 4.2.1.1. 输出文件''' [[BR]][[BR]] To make a new file, call {{{open}}} with two arguments: the external name of the file to be created and a mode string {{{w}}} (short for write). To store data on the file, call the file object's {{{write}}} method with a string containing the data to store, and then call the {{{close}}} method to close the file if you wish to open it again within the same program or session: |
Line 43: | Line 27: |
C:\temp> >>> # open output file object: creates >>> # writes strings verbatim >>> >>> # closed on gc and exit too |
C:\temp> python >>> file = open('data.txt', 'w') # open output file object: creates >>> file.write('Hello file world!\n') # writes strings verbatim >>> file.write('Bye file world.\n') >>> file.close( ) # closed on gc and exit too |
Line 54: | Line 38: |
C:\temp> | C:\temp> dir data.txt /B |
Line 56: | Line 40: |
C:\temp> | C:\temp> type data.txt |
Line 64: | Line 48: |
''' 4.2.1.1.1. Opening ''' [[BR]] ''' 4.2.1.1.1. 打开文件 ''' [[BR]][[BR]] In the {{{open}}} function call shown in the preceding example, the first argument can optionally specify a complete directory path as part of the filename string. If we pass just a simple filename without a path, the file will appear in Python's current working directory. That is, it shows up in the place where the code is run. Here, the directory C:\temp on my machine is implied by the bare filename data.txt, so this actually creates a file at C:\temp\data.txt. More accurately, the filename is relative to the current working directory if it does not include a complete absolute directory path. See the section "[file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-3-SECT-6.html#python3-CHP-3-SECT-6 Current Working Directory]," in [file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-3.html#python3-CHP-3 Chapter 3], for a refresher on this topic. |
[[BR]] ''' 4.2.1.1.1. Opening ''' [[BR]] ''' 4.2.1.1.1. 打开文件 ''' [[BR]][[BR]] In the {{{open}}} function call shown in the preceding example, the first argument can optionally specify a complete directory path as part of the filename string. If we pass just a simple filename without a path, the file will appear in Python's current working directory. That is, it shows up in the place where the code is run. Here, the directory C:\temp on my machine is implied by the bare filename data.txt, so this actually creates a file at C:\temp\data.txt. More accurately, the filename is relative to the current working directory if it does not include a complete absolute directory path. See the section "[Current Working Directory]," in [file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-3.html#python3-CHP-3 Chapter 3], for a refresher on this topic. |
Line 70: | Line 50: |
在前面的{{{open}}}函数用法的演示中, 函数的第一个参数将文件的绝对路径作为文件名传入.如果我们文件名参数仅传入文件名.新的文件将会出现在Python的当前工作目录.也就是代码运行的目 录,所以要我系统的C:\temp隐含地说明代码将会生成C:\temp\data.txt的文件,更准确地说,如果不传入绝对路径.文件名就是基于当前 工作目录的.可以在 [file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-3.html#python3-CHP-3 第3章]的 [file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-3-SECT-6.html#python3-CHP-3-SECT-6 当前工作目录]重新温习这一个内容. | 在前面的{{{open}}}函数用法的演示中, 函数的第一个参数将文件的绝对路径作为文件名传入.如果我们文件名参数仅传入文件名.新的文件将会出现在Python的当前工作目录.也就是代码运行的目 录,所以要我系统的C:\temp隐含地说明代码将会生成C:\temp\data.txt的文件,更准确地说,如果不传入绝对路径.文件名就是基于当前 工作目录的.可以在 [第3章]的 [当前工作目录]重新温习这一个内容. |
Line 72: | Line 52: |
Also note that when opening in {{{w}}} mode, Python either creates the external file if it does not yet exist or erases the file's current contents if it is already present on your machine (so be careful out thereyou'll delete whatever was in the file before). | [[BR]] Also note that when opening in {{{w}}} mode, Python either creates the external file if it does not yet exist or erases the file's current contents if it is already present on your machine (so be careful out thereyou'll delete whatever was in the file before). |
Line 76: | Line 56: |
''' 4.2.1.1.2. Writing ''' [[BR]] ''' 4.2.1.1.2. 写入 ''' [[BR]][[BR]] |
[[BR]] ''' 4.2.1.1.2. Writing ''' [[BR]] ''' 4.2.1.1.2. 写入 ''' [[BR]][[BR]] Notice that we added an explicit {{{\n}}} end-of-line character to lines written to the file; unlike the {{{print}}} statement, file {{{write}}} methods write exactly what they are passed without adding any extra formatting. The string passed to {{{write}}} shows up byte for byte on the external file. 你应该可以注意到我们在上面写入文件的每行行尾加了个{{{\n}}}; 和{{{print}}} 不同的是, {{{write}}} 将传入的字符串不做任何格式处理地输出. 因此{{{write}}} 写入文件的字符与传入的显示是一样的. Output files also sport a {{{writelines}}} method, which simply writes all of the strings in a list one at a time without adding any extra formatting. For example, here is a {{{writelines}}} equivalent to the two {{{write}}} calls shown earlier: 输出至文件同样也支持{{{writelines}}}方法, 它一次将链表中的所有字符串不做任何格式处理地输出至文件中.例如, 下面使用的{{{writelines}}}相当于前面的两次{{{write}}}调用: {{{ }}} This call isn't as commonly used (and can be emulated with a simple {{{for}}} loop), but it is convenient in scripts that save output in a list to be written later. 这种调用并不常用(可以通过一个简单的{{{for}}}循环来实现), 不过在脚本中常用的方式是将要输出的东西存入一个链表中以备输出. ==== 4.2.1.1.3. Closing ==== ==== 4.2.1.1.3. 关闭文件 ==== The file {{{close}}} method used earlier finalizes file contents and frees up system resources. For instance, closing forces buffered output data to be flushed out to disk. Normally, files are automatically closed when the file object is garbage collected by the interpreter (i.e., when it is no longer referenced) and when the Python session or program exits. Because of that, {{{close}}} calls are often optional. In fact, it's common to see file-processing code in Python like this: 文件对象的{{{close}}}方法用来结束文件的内容并且释放系统资源. 诸如, 关闭文件强行将缓冲中的数据输出到硬盘.正常情况下 , 当解释器(目前没有参考)将文件对象进行垃圾回收,Python会话结束,或当前程序结束时,文件对象将会自动关闭. 正因如此, {{{close}}}常常被作为可选操作. 事实上,我们常常会看到Python中这样的文件处理过程: {{{ open('somefile.txt', 'w').write("G'day Bruce\n") }}} Since this expression makes a temporary file object, writes to it immediately, and does not save a reference to it, the file object is reclaimed and closed right away without ever having called the {{{close}}} method explicitly. 上面的表达式生成一个临时的文件对象, 并马上输出, 这种文件对象不会生成一个对象参考, 临时文件对象并没有调用{{{close}}}方法.但它已经被正常地回收并关闭了. ||<tablebgcolor="black" tablewidth="90%" tablealign="center"> ||<tablebgcolor="white" tablewidth="100%"^ width="60px"> ||<style="vertical-align: top;">But note that this auto-close on reclaim file feature may change in future Python releases. Moreover, the Jython Java-based Python implementation discussed later does not reclaim files as immediately as the standard Python system (it uses Java's garbage collector). If your script makes many files and your platform limits the number of open files per program, explicit {{{close}}} calls are a robust habit to form. 需要注意的是这种对于回收文件的自动关闭 特性可能会在将来的Python版本中有所改变. 而且, 后面我们讨论到的基于Java的Python实现Jthon并不像Python系统这样立即回收文件(它使用Java垃圾回收器). 如果你打开太多的文件并且你的系统对打开文件的数目有限制, 显式调用{{{close}}}是一个应该养成的习惯. Also note that some IDEs, such as Python's standard IDLE GUI, may hold on to your file objects longer than you expect, and thus prevent them from being garbage collected. If you write to an output file in IDLE, be sure to explicitly close (or flush) your file if you need to read it back in the same IDLE session. Otherwise, output buffers won't be flushed to disk and your file may be incomplete when read. 并且需要注意的是很多的Python IDEs, 如Pythonr的标准IDE: IDLE GUI,可能持有你打开的文件比你期望的时间要长,阻止文件被垃圾回收掉.如果你在IDLE中打开需要输出的文件, 并需要在同一IDLE会话中再次打开读取它,应该对文件显式地调用close (或 flush).否则, 输出缓冲将不会被写到硬盘,再次读入的文件时可能是不完整的. || || ==== 4.2.1.2. Input files ==== ==== 4.2.1.2. 读取文件 ==== Reading data from external files is just as easy as writing, but there are more methods that let us load data in a variety of modes. Input text files are opened with either a mode flag of {{{r}}} (for "read") or no mode flag at allit defaults to {{{r}}} if omitted, and it commonly is. Once opened, we can read the lines of a text file with the {{{readlines}}} method: 从外部文件读取文件和写入一样简单,但是我们可通过更多的方法以不同模式打开文本文件, . 可以以{{{r}}} ("read")模式或者不指定, 不指定模式时默认为{{{r}}}, 且通常这样使用. 文件打开后,我们可以调用{{{readlines}}}方法从文本文件中读入多行 : {{{ >>> # open input file object >>> # read into line string list ... # lines have '\n' at end ... Hello file world! Bye file world. }}} The {{{readlines}}} method loads the entire contents of the file into memory and gives it to our scripts as a list of line strings that we can step through in a loop. In fact, there are many ways to read an input file: {{{readlines}}}方法将全部文件加载到内存,表现给用户一个由文件每行文字组成的链表.从而使用户可以通过循环依次读取. 事实上,我们可以通过很多方法来读取文件: |
4.2. File Tools BR 4.2. 文件工具 BRBR External files are at the heart of much of what we do with shell utilities. For instance, a testing system may read its inputs from one file, store program results in another file, and check expected results by loading yet another file. Even user interface and Internet-oriented programs may load binary images and audio clips from files on the underlying computer. It's a core programming concept.
处理外部文件是我们在Shell中最常做的工作. 例如, 一个测试系统可能需要读入一个文件, 将程序的结果存放在另一个文件中.并且可能需要读入其他文件来检查期望的结果. 甚至用户界面和网络程序可能需要在本地系统载入二进制图片和声音剪辑. 处理文件是这些程序的核心内容.
BR In Python, the built-in open function is the primary tool scripts use to access the files on the underlying computer system. Since this function is an inherent part of the Python language, you may already be familiar with its basic workings. Technically, open gives direct access to the stdio filesystem calls in the system's C libraryit returns a new file object that is connected to the external file and has methods that map more or less directly to file calls on your machine. The open function also provides a portable interface to the underlying filesystemit works the same way on every platform on which Python runs.
在Python中, 内建函数open是访问本地系统文件的主要脚本工具. 这个函数是Python与生俱来的, 你可能已基本熟悉了它的功能. 技术实现上, open 通过调用系统C库直接访问stdio文件系统, 它将返回一个连接到外部文件的新的文件对象. 这个对象的很多方法直接或间接地映射到你系统的调用上. open 函数同样提供一个本地文件系统的一个可移植接口,它在所有Python运行的平台上以同样的方式调用执行.
BR Other file-related interfaces in Python allow us to do things such as manipulate lower-level descriptor-based files (os module), store objects away in files by key (anydbm and shelve modules), and access SQL databases. Most of these are larger topics addressed in Ch-19-1.
Pyhton中其他文件相关的接口允许我们处理基于底层的基于描述符的文件(os模块),将对象以索引的方式存储在文件中.(anydbm and shelve 模块), 访问SQL数据库. 这些主题将在Ch-19-1详细讲解.
BR In this chapter, we'll take a brief tutorial look at the built-in file object and explore a handful of more advanced file-related topics. As usual, you should consult the library manual's file object entry for further details and methods we don't have space to cover here. Remember, for quick interactive help, you can also run dir(file) for an attributes list with methods, help(file) for general help, and help(file.read) for help on a specific method such as read. The built-in name file identifies the file datatype in recent Python releases.[[*]]
本章中, 我们将通过一个简单的教程认识内建的文件对象和少量高级的文件相关主题. 同样, 你应当通过查阅对象指导文件学习更详细的信息和方法.我们这里没有太多的章节去覆盖, 注意, ,在交互中,你可以通过运行dir(file)来得到方法属性的列表,从而更快地得到帮助. help(file) 来得到基本帮助信息, 同样help(file.read) 可以得到某个方法的详细帮助.例如read.在最近的Python发行版中. 内建的file 用来标识file数据类型.[[*]]
Technically, you can use the name file anywhere you use open, though open is still the generally preferred call unless you are subclassing to customize files. We'll use open in most of our examples. As for all built-in names, it's OK to use the name file for your own variables as long as you don't need direct access to the built-in file datatype (your file name will hide the built-in scope's file). In fact, this is such a common practice that we'll frequently follow it here. This is not a sin, but you should generally be careful about reusing built-in names in this way. 技术上讲, 你可以在所有你用open的地方使用file, 因此open仍然是常用的调用方法除非你通用子类来定制它.我们的大部分例子使用open. 对于内建的名称, 只有当你不需要访问内建file数据类型时你可以将file作为你的变量(你的file将会覆盖内建范围的file). 事实上.这是一种常用的实践做法.我们应遵循它, 这并不是种罪恶,当你需要重用内建的名称时应注意.
4.2.1. Built-In File Objects BR 4.2.1. 内建文件对象 BRBR For most purposes, the open function is all you need to remember to process files in your scripts. The file object returned by open has methods for reading data (read, readline, readlines), writing data (write, writelines), freeing system resources (close), moving about in the file (seek), forcing data to be transferred out of buffers (flush), fetching the underlying file handle (fileno), and more. Since the built-in file object is so easy to use, though, let's jump right into a few interactive examples.
在大部分应用中, 你的脚本只需要用到open函数处理文件. open函数返回的文件对象有多种读入数据的方法(read, readline, readlines), 写入数据(write, writelines), 释放系统资源(close), 文件内容中移动(seek), 强制清除缓存中的数据(flush), 取得本地文件句柄(fileno), 以及其他功能. 文件对象的使用是相当简单的.让我们做一些的例子.
BR 4.2.1.1. Output files BR 4.2.1.1. 输出文件 BRBR To make a new file, call open with two arguments: the external name of the file to be created and a mode string w (short for write). To store data on the file, call the file object's write method with a string containing the data to store, and then call the close method to close the file if you wish to open it again within the same program or session:
为创建一个新文件, 你可以调用open方法并传入两个参数: 要创建文件的name 和打开文件模式参数mode w (write的缩写). 为将数据存入文件, 调用打开文件的write方法,并将要写入的数据用字符串传入.如果需要在同一程序或会话中再次打开文件.可以调用close将文件关闭:
C:\temp> python >>> file = open('data.txt', 'w') # open output file object: creates >>> file.write('Hello file world!\n') # writes strings verbatim >>> file.write('Bye file world.\n') >>> file.close( ) # closed on gc and exit too
And that's ityou've just generated a brand-new text file on your computer, regardless of the computer on which you type this code:
不论你在何种系统上键入这一段代码,它都会在你在系统上生成一个新的文本文件:
C:\temp> dir data.txt /B data.txt C:\temp> type data.txt Hello file world! Bye file world.
There is nothing unusual about the new file; here, I use the DOS dir and type commands to list and display the new file, but it shows up in a file explorer GUI too.
这个文件与其他的新建文件没什么不同, 我们可以利用DOS下的dir and type 来浏览和查看文件, 同时它也可以在图形界面的文件浏览器中查看到.
BR 4.2.1.1.1. Opening BR 4.2.1.1.1. 打开文件 BRBR In the open function call shown in the preceding example, the first argument can optionally specify a complete directory path as part of the filename string. If we pass just a simple filename without a path, the file will appear in Python's current working directory. That is, it shows up in the place where the code is run. Here, the directory C:\temp on my machine is implied by the bare filename data.txt, so this actually creates a file at C:\temp\data.txt. More accurately, the filename is relative to the current working directory if it does not include a complete absolute directory path. See the section "[Current Working Directory]," in [file:///home/piao/Programing/Python/ProgrammingPython/0596009259/python3-CHP-3.html#python3-CHP-3 Chapter 3], for a refresher on this topic.
在前面的open函数用法的演示中, 函数的第一个参数将文件的绝对路径作为文件名传入.如果我们文件名参数仅传入文件名.新的文件将会出现在Python的当前工作目录.也就是代码运行的目 录,所以要我系统的C:\temp隐含地说明代码将会生成C:\temp\data.txt的文件,更准确地说,如果不传入绝对路径.文件名就是基于当前 工作目录的.可以在 [第3章]的 [当前工作目录]重新温习这一个内容.
BR Also note that when opening in w mode, Python either creates the external file if it does not yet exist or erases the file's current contents if it is already present on your machine (so be careful out thereyou'll delete whatever was in the file before).
需要声明的是,当你在w模式用open打开文件时,如果文件不存在.它会创建新数据,如果文件已经存在.它将会擦写该文件(因此需要小心.不论文件之前包含什么.它都会将其删除).
BR 4.2.1.1.2. Writing BR 4.2.1.1.2. 写入 BRBR
Notice that we added an explicit \n end-of-line character to lines written to the file; unlike the print statement, file write methods write exactly what they are passed without adding any extra formatting. The string passed to write shows up byte for byte on the external file.
你应该可以注意到我们在上面写入文件的每行行尾加了个\n; 和print 不同的是, write 将传入的字符串不做任何格式处理地输出. 因此write 写入文件的字符与传入的显示是一样的.
Output files also sport a writelines method, which simply writes all of the strings in a list one at a time without adding any extra formatting. For example, here is a writelines equivalent to the two write calls shown earlier:
输出至文件同样也支持writelines方法, 它一次将链表中的所有字符串不做任何格式处理地输出至文件中.例如, 下面使用的writelines相当于前面的两次write调用:
This call isn't as commonly used (and can be emulated with a simple for loop), but it is convenient in scripts that save output in a list to be written later.
这种调用并不常用(可以通过一个简单的for循环来实现), 不过在脚本中常用的方式是将要输出的东西存入一个链表中以备输出.
4.2.1.1.3. Closing
4.2.1.1.3. 关闭文件
The file close method used earlier finalizes file contents and frees up system resources. For instance, closing forces buffered output data to be flushed out to disk. Normally, files are automatically closed when the file object is garbage collected by the interpreter (i.e., when it is no longer referenced) and when the Python session or program exits. Because of that, close calls are often optional. In fact, it's common to see file-processing code in Python like this:
文件对象的close方法用来结束文件的内容并且释放系统资源. 诸如, 关闭文件强行将缓冲中的数据输出到硬盘.正常情况下 , 当解释器(目前没有参考)将文件对象进行垃圾回收,Python会话结束,或当前程序结束时,文件对象将会自动关闭. 正因如此, close常常被作为可选操作. 事实上,我们常常会看到Python中这样的文件处理过程:
open('somefile.txt', 'w').write("G'day Bruce\n")
Since this expression makes a temporary file object, writes to it immediately, and does not save a reference to it, the file object is reclaimed and closed right away without ever having called the close method explicitly.
上面的表达式生成一个临时的文件对象, 并马上输出, 这种文件对象不会生成一个对象参考, 临时文件对象并没有调用close方法.但它已经被正常地回收并关闭了.
||<tablebgcolor="black" tablewidth="90%" tablealign="center"> ||<tablebgcolor="white" tablewidth="100%"^ width="60px">
But note that this auto-close on reclaim file feature may change in future Python releases. Moreover, the Jython Java-based Python implementation discussed later does not reclaim files as immediately as the standard Python system (it uses Java's garbage collector). If your script makes many files and your platform limits the number of open files per program, explicit close calls are a robust habit to form. 需要注意的是这种对于回收文件的自动关闭 特性可能会在将来的Python版本中有所改变. 而且, 后面我们讨论到的基于Java的Python实现Jthon并不像Python系统这样立即回收文件(它使用Java垃圾回收器). 如果你打开太多的文件并且你的系统对打开文件的数目有限制, 显式调用close是一个应该养成的习惯. Also note that some IDEs, such as Python's standard IDLE GUI, may hold on to your file objects longer than you expect, and thus prevent them from being garbage collected. If you write to an output file in IDLE, be sure to explicitly close (or flush) your file if you need to read it back in the same IDLE session. Otherwise, output buffers won't be flushed to disk and your file may be incomplete when read. 并且需要注意的是很多的Python IDEs, 如Pythonr的标准IDE: IDLE GUI,可能持有你打开的文件比你期望的时间要长,阻止文件被垃圾回收掉.如果你在IDLE中打开需要输出的文件, 并需要在同一IDLE会话中再次打开读取它,应该对文件显式地调用close (或 flush).否则, 输出缓冲将不会被写到硬盘,再次读入的文件时可能是不完整的. |
||
4.2.1.2. Input files
4.2.1.2. 读取文件
Reading data from external files is just as easy as writing, but there are more methods that let us load data in a variety of modes. Input text files are opened with either a mode flag of r (for "read") or no mode flag at allit defaults to r if omitted, and it commonly is. Once opened, we can read the lines of a text file with the readlines method:
从外部文件读取文件和写入一样简单,但是我们可通过更多的方法以不同模式打开文本文件, . 可以以r ("read")模式或者不指定, 不指定模式时默认为r, 且通常这样使用. 文件打开后,我们可以调用readlines方法从文本文件中读入多行 :
>>> # open input file object >>> # read into line string list ... # lines have '\n' at end ... Hello file world! Bye file world.
The readlines method loads the entire contents of the file into memory and gives it to our scripts as a list of line strings that we can step through in a loop. In fact, there are many ways to read an input file:
readlines方法将全部文件加载到内存,表现给用户一个由文件每行文字组成的链表.从而使用户可以通过循环依次读取. 事实上,我们可以通过很多方法来读取文件: