← 学习库 Think Python (2e) · 中英对照 目录

Chapter 14  Files 第 14 章 文件

本页译自 Think Python 2e(Allen B. Downey)· Chapter 14 Files。代码块保留英文原文不翻译;正文段段对照,中文块可用右下角按钮隐藏。

14.1 Persistence 14.1 持久化

Most of the programs we have seen so far are transient in the sense that they run for a short time and produce some output, but when they end, their data disappears. If you run the program again, it starts with a clean slate.

到目前为止,我们看到的大多数程序都是「短暂」的:它们运行一小段时间、产生一些输出,但程序一结束,数据就消失了。再运行一次,又是从零开始。

Other programs are persistent: they run for a long time (or all the time); they keep at least some of their data in permanent storage (a hard drive, for example); and if they shut down and restart, they pick up where they left off.

另一些程序是持久的:它们运行很久(甚至一直运行);至少把一部分数据保存在永久存储里(比如硬盘);即便关机重启,也能从离开的地方接着干。

Examples of persistent programs are operating systems, which run pretty much whenever a computer is on, and web servers, which run all the time, waiting for requests to come in on the network.

持久化程序的例子有操作系统(只要电脑开着它基本就在跑)和 Web 服务器(全天候运行,等着网络上发来的请求)。

One of the simplest ways for programs to maintain their data is by reading and writing text files. We have already seen programs that read text files; in this chapter we will see programs that write them.

程序保存数据最简单的办法之一,就是读写文本文件。读文本的我们已经见过了;本章要看的是写文本的程序。

An alternative is to store the state of the program in a database. In this chapter I will present a simple database and a module, pickle, that makes it easy to store program data.

另一个办法是把程序的状态存进数据库。本章我会介绍一个简单的数据库,以及一个叫 pickle 的模块,用它们来保存程序数据都很轻松。

14.2 Reading and writing 14.2 读与写

A text file is a sequence of characters stored on a permanent medium like a hard drive, flash memory, or CD-ROM. We saw how to open and read a file in Section 9.1.

文本文件是一串字符,保存在硬盘、闪存或 CD-ROM 这类永久介质上。在 9.1 节我们看过如何打开并读取文件。

To write a file, you have to open it with mode 'w' as a second parameter:

要写文件,得用 'w' 作为第二个参数来打开它:
pickle008

If the file already exists, opening it in write mode clears out the old data and starts fresh, so be careful! If the file doesn’t exist, a new one is created.

如果文件已存在,以写模式打开会清空旧数据、重新开始,所以小心了!如果文件不存在,则会新建一个。

The 'w'00 method puts data into the file.

'w'00 方法把数据写进文件。
pickle011

Again, the file object keeps track of where it is, so if you call 'w'00 again, it adds the new data to the end.

同样地,文件对象会记住自己写到了哪里,所以再调用一次 'w'00,新数据会接在末尾。
pickle014

When you are done writing, you have to close the file.

写完之后,得把文件关掉。
pickle015

14.3 Format operator 14.3 格式化运算符

The argument of 'w'00 has to be a string, so if we want to put other values in a file, we have to convert them to strings. The easiest way to do that is with 'w':

'w'00 的参数必须是字符串,所以想把别的值写进文件,得先转成字符串。最简单的办法是用 'w'
pickle020

An alternative is to use the format operator, %. When applied to integers, % is the modulus operator. But when the first operand is a string, % is the format operator.

另一个办法是用格式化运算符 %。作用于整数时,% 是求模运算符;但当第一个操作数是字符串时,% 就是格式化运算符。

The first operand is the format string, which contains one or more format sequences, which specify how the second operand is formatted. The result is a string.

第一个操作数是格式化字符串,里面含有一个或多个格式化序列,用来指定第二个操作数怎么格式化。结果是一个字符串。

For example, the format sequence 'w'0 means that the second operand should be formatted as an integer (% stands for “decimal”):

例如,格式化序列 'w'0 表示第二个操作数要按整数格式输出(% 代表「decimal」,十进制):
pickle031

The result is the string 'w'0, which is not to be confused with the integer value %0.

结果是字符串 'w'0,可别和整数值 %0 搞混。

A format sequence can appear anywhere in the string, so you can embed a value in a sentence:

格式化序列可以出现在字符串的任何地方,所以能把一个值嵌进句子里:
pickle036

If there is more than one format sequence in the string, the second argument has to be a tuple. Each format sequence is matched with an element of the tuple, in order.

如果字符串里有不止一个格式化序列,第二个参数就得是一个元组。每个格式化序列按顺序和元组里的元素一一对应。

The following example uses 'w'0 to format an integer, 'w'0 to format a floating-point number (don’t ask why), and 'w'0 to format a string:

下面的例子用 'w'0 格式化整数、用 'w'0 格式化浮点数(别问为什么)、用 'w'0 格式化字符串:
pickle043

The number of elements in the tuple has to match the number of format sequences in the string. Also, the types of the elements have to match the format sequences:

元组里元素的个数必须和字符串中格式化序列的个数一致。而且元素的类型也得和格式化序列对应上:
pickle044

In the first example, there aren’t enough elements; in the second, the element is the wrong type.

第一个例子里元素不够;第二个例子里元素类型不对。

The format operator is powerful, but it can be difficult to use. You can read more about it at pickle045.

格式化运算符很强大,但用起来也有难度。想深入了解,可以看 pickle046。

14.4 Filenames and paths 14.4 文件名与路径

Files are organized into directories (also called “folders”). Every running program has a “current directory,” which is the default directory for most operations. For example, when you open a file for reading, Python looks for it in the current directory.

文件被组织进目录(也叫「文件夹」)里。每个运行中的程序都有一个「当前目录」,它是大多数操作的默认目录。比如你打开一个文件来读时,Python 会在当前目录里找它。

The %0 module provides functions for working with files and directories (“os” stands for “operating system”). pickle048 returns the name of the current directory:

%0 模块提供了操作文件和目录的函数(「os」是「operating system」的缩写)。pickle050 返回当前目录的名字:
pickle051

'w' stands for “current working directory.” The result in this example is pickle053, which is the home directory of a user named pickle05.

'w' 是「current working directory」(当前工作目录)的缩写。这个例子的结果是 pickle056,那是名叫 pickle05 的用户的主目录。

A string like 'w' that identifies a file is called a path. A relative path starts from the current directory; an absolute path starts from the topmost directory in the file system.

'w' 这样用来标识一个文件的字符串,叫做路径相对路径从当前目录开始;绝对路径从文件系统最顶层的目录开始。

The paths we have seen so far are simple filenames, so they are relative to the current directory. To find the absolute path to a file, you can use pickle060:

到目前为止我们看到的路径都是简单的文件名,所以它们是相对于当前目录的。要找出一个文件的绝对路径,可以用 pickle061:
pickle062

pickle063 checks whether a file or directory exists:

pickle064 检查某个文件或目录是否存在:
pickle065

If it exists, pickle066 checks whether it’s a directory:

如果存在,pickle067 会检查它是不是一个目录:
pickle068

Similarly, pickle069 checks whether it’s a file.

同理,pickle070 检查它是不是一个文件。

pickle071 returns a list of the files (and other directories) in the given directory:

pickle072 返回给定目录下的文件(及其他目录)列表:
pickle073

To demonstrate these functions, the following example “walks” through a directory, prints the names of all the files, and calls itself recursively on all the directories.

为了演示这些函数,下面这个例子会「走遍」一个目录,打印所有文件名,并对所有子目录递归调用自身。
pickle074

pickle075 takes a directory and a file name and joins them into a complete path.

pickle076 接收一个目录和一个文件名,把它们拼成一个完整的路径。

Exercise 1

习题 1

The %0 module provides a function called 'w'0 that is similar to this one but more versatile. Read the documentation and use it to print the names of the files in a given directory and its subdirectories.

%0 模块里也有个叫 'w'0 的函数,和上面这个很像但更通用。读读文档,用它打印出给定目录及其所有子目录里的文件名。

Solution: pickle081.

参考解答:pickle082。

14.5 Catching exceptions 14.5 捕获异常

A lot of things can go wrong when you try to read and write files. If you try to open a file that doesn’t exist, you get an pickle0:

读写文件时,出错的机会可不少。要是试着打开一个不存在的文件,就会遇到 pickle0:
pickle085

If you don’t have permission to access a file:

如果你没有权限访问某个文件:
pickle086

And if you try to open a directory for reading, you get

而如果你试着打开一个目录来读,会得到:
pickle087

To avoid these errors, you could use functions like pickle088 and pickle089, but it would take a lot of time and code to check all the possibilities (if “pickle09” is any indication, there are at least 21 things that can go wrong).

要避免这些错误,你可以用 pickle091、pickle092 这类函数,但要把所有可能性都检查一遍,既费时又费代码(光看「pickle09」就知道,至少能出 21 种岔子)。

It is better to go ahead and try—and deal with problems if they happen—which is exactly what the 'w' statement does. The syntax is similar to an %0 statement:

与其这样,不如直接去试——真出问题再处理——这正是 'w' 语句的做派。它的语法和 %0 语句有点像:
pickle098

Python starts by executing the 'w' clause. If all goes well, it skips the pickle clause and proceeds. If an exception occurs, it jumps out of the 'w' clause and executes the pickle clause.

Python 先执行 'w' 子句。一切顺利的话就跳过 pickle 子句继续往下走;一旦抛出异常,它就跳出 'w' 子句,转去执行 pickle 子句。

Handling an exception with a 'w' statement is called catching an exception. In this example, the pickle clause prints an error message that is not very helpful. In general, catching an exception gives you a chance to fix the problem, or try again, or at least end the program gracefully.

'w' 语句来处理异常,叫做捕获异常。这个例子里,pickle 子句打印的错误信息其实没什么用。一般地说,捕获异常给了你一个修补问题、重试、或者至少体面退出的机会。

Exercise 2

习题 2

Write a function called 'w' that takes as arguments a pattern string, a replacement string, and two filenames; it should read the first file and write the contents into the second file (creating it if necessary). If the pattern string appears anywhere in the file, it should be replaced with the replacement string.

写一个叫 'w' 的函数,参数为一个模式串、一个替换串和两个文件名;它读取第一个文件,把内容写入第二个文件(必要时创建之)。只要模式串在文件里出现,就用替换串把它换掉。

If an error occurs while opening, reading, writing or closing files, your program should catch the exception, print an error message, and exit. Solution: pickle113.

若在打开、读取、写入或关闭文件时出错,程序应捕获异常、打印错误信息并退出。参考解答:pickle114。

14.6 Databases 14.6 数据库

A database is a file that is organized for storing data. Most databases are organized like a dictionary in the sense that they map from keys to values. The biggest difference is that the database is on disk (or other permanent storage), so it persists after the program ends.

数据库是一种为存储数据而组织的文件。多数数据库的组织方式像字典:键映射到值。最大的区别是,数据库在磁盘(或其他永久存储)上,所以程序结束后它依然存在。

The module pickle provides an interface for creating and updating database files. As an example, I’ll create a database that contains captions for image files.

pickle 模块提供了创建和更新数据库文件的接口。作为例子,我来建一个存放图片说明文字的数据库。

Opening a database is similar to opening other files:

打开数据库和打开其他文件差不多:
pickle117

The mode 'w' means that the database should be created if it doesn’t already exist. The result is a database object that can be used (for most operations) like a dictionary. If you create a new item, pickle updates the database file.

模式 'w' 表示:如果数据库还不存在就创建它。结果是一个数据库对象,大多数操作上可以当字典用。一旦新建一项,pickle 就会更新数据库文件。
pickle122

When you access one of the items, pickle reads the file:

当你访问其中一项时,pickle 会去读文件:
pickle125

If you make another assignment to an existing key, pickle replaces the old value:

如果对已有键再赋值,pickle 会替换掉旧值:
pickle128

Many dictionary methods, like 'w'0 and 'w'00, also work with database objects. So does iteration with a 'w' statement.

许多字典方法,比如 'w'0 和 'w'00,对数据库对象也管用。用 'w' 语句遍历也一样。
pickle135

As with other files, you should close the database when you are done:

和其他文件一样,用完之后应当关掉数据库:
pickle136

14.7 Pickling 14.7 序列化(pickle)

A limitation of pickle is that the keys and values have to be strings. If you try to use any other type, you get an error.

pickle 有个限制:键和值都必须是字符串。想用其他类型就会报错。

The pickle module can help. It translates almost any type of object into a string suitable for storage in a database, and then translates strings back into objects.

pickle 模块能帮上忙。它几乎能把任何类型的对象转成适合存进数据库的字符串,也能把字符串还原回对象。

pickle141 takes an object as a parameter and returns a string representation ('w'00 is short for “dump string”):

pickle143 把对象当参数,返回它的字符串表示('w'00 是「dump string」的缩写):
pickle145

The format isn’t obvious to human readers; it is meant to be easy for pickle to interpret. pickle147 (“load string”) reconstitutes the object:

这个格式人看着费劲,但它专为 pickle 好解读而设计。pickle149(「load string」)能把对象重新还原出来:
pickle150

Although the new object has the same value as the old, it is not (in general) the same object:

虽然新对象的值和旧对象一样,但它(一般说来)并不是同一个对象:
pickle151

In other words, pickling and then unpickling has the same effect as copying the object.

换句话说,先 pickle 再 unpickle,效果和拷贝对象一样。

You can use pickle to store non-strings in a database. In fact, this combination is so common that it has been encapsulated in a module called pickle.

可以用 pickle 把非字符串存进数据库。事实上这组合太常见了,已经被封装成了一个叫 pickle 的模块。

Exercise 3

习题 3

If you download my solution to Exercise 4 from pickle156, you’ll see that it creates a dictionary that maps from a sorted string of letters to the list of words that can be spelled with those letters. For example, pickle maps to the list pickle158.

如果你下载我针对习题 4 的解答 pickle159,会发现它创建了一个字典,把字母排序后的串映射到能用这些字母拼出的单词列表。比如 pickle 映射到列表 pickle161。

Write a module that imports pickle162 and provides two new functions: pickle163 should store the anagram dictionary in a “shelf;” pickle164 should look up a word and return a list of its anagrams. Solution: pickle165

写一个模块,导入 pickle166 并提供两个新函数:pickle167 把变位词字典存进一个「shelf」;pickle168 查一个单词并返回它的变位词列表。参考解答:pickle169

14.8 Pipes 14.8 管道

Most operating systems provide a command-line interface, also known as a shell. Shells usually provide commands to navigate the file system and launch applications. For example, in Unix you can change directories with %0, display the contents of a directory with %0, and launch a web browser by typing (for example) pickle1.

多数操作系统都提供一个命令行界面,也叫命令行 shell。shell 通常提供在文件系统里穿梭、启动应用程序的命令。比如在 Unix 里,你可以用 %0 切换目录、用 %0 列出目录内容、敲一句(比如)pickle1 就能启动浏览器。

Any program that you can launch from the shell can also be launched from Python using a pipe. A pipe is an object that represents a running program.

任何能从 shell 启动的程序,也能用 管道 从 Python 里启动。管道是一个代表「正在运行的程序」的对象。

For example, the Unix command 'w'00 normally displays the contents of the current directory (in long format). You can launch %0 with pickle171:

比如 Unix 命令 'w'00 通常用来(以长格式)显示当前目录的内容。你可以用 pickle181 启动 %0:
pickle182

The argument is a string that contains a shell command. The return value is an object that behaves just like an open file. You can read the output from the %0 process one line at a time with pickle18 or get the whole thing at once with 'w'0:

参数是一个包含 shell 命令的字符串。返回值的行为就像一个打开的文件。你可以用 pickle18 一次读一行 %0 进程的输出,也可以用 'w'0 一口气全读出来:
pickle189

When you are done, you close the pipe like a file:

用完之后,像关文件一样关掉管道:
pickle190

The return value is the final status of the %0 process; 'w'0 means that it ended normally (with no errors).

返回值是 %0 进程的最终状态;'w'0 表示它正常结束(没出错)。

For example, most Unix systems provide a command called pickle that reads the contents of a file and computes a “checksum.” You can read about MD5 at pickle196. This command provides an efficient way to check whether two files have the same contents. The probability that different contents yield the same checksum is very small (that is, unlikely to happen before the universe collapses).

比如大多数 Unix 系统都有个叫 pickle 的命令,读入文件内容并算出「校验和」。关于 MD5 可以看 pickle198。这个命令能高效地判断两个文件内容是否相同。不同内容算出相同校验和的概率极小(也就是说,在宇宙坍缩之前基本不会发生)。

You can use a pipe to run pickle from Python and get the result:

你可以用管道从 Python 里跑 pickle 并拿到结果:
pickle201

Exercise 4

习题 4

In a large collection of MP3 files, there may be more than one copy of the same song, stored in different directories or with different file names. The goal of this exercise is to search for duplicates.

一大堆 MP3 文件里,同一首歌可能不止一份,分别存在不同目录或用不同文件名。这道习题的目标就是找出重复文件。
  1. Write a program that searches a directory and all of its subdirectories, recursively, and returns a list of complete paths for all files with a given suffix (like 'w'0). Hint: pickle2 provides several useful functions for manipulating file and path names.
  1. 写一个程序,递归地搜索一个目录及其所有子目录,返回所有带指定后缀(如 'w'0)文件的完整路径列表。提示:pickle2 提供了几个处理函数名和路径名的有用函数。
  1. To recognize duplicates, you can use pickle to compute a “checksum” for each files. If two files have the same checksum, they probably have the same contents.
  1. 要识别重复,可以用 pickle 为每个文件算一个「校验和」。两个文件校验和相同,它们内容多半也相同。
  1. To double-check, you can use the Unix command 'w'0.
  1. 想再确认一下,可以用 Unix 命令 'w'0。

Solution: pickle210.

参考解答:pickle211。

14.9 Writing modules 14.9 编写模块

Any file that contains Python code can be imported as a module. For example, suppose you have a file named 'w'00 with the following code:

任何包含 Python 代码的文件都能当作模块导入。比如你有个叫 'w'00 的文件,内容如下:
pickle214

If you run this program, it reads itself and prints the number of lines in the file, which is 7. You can also import it like this:

如果直接运行这个程序,它会读取自己,打印出文件的行数,也就是 7。你也可以这样导入它:
pickle215

Now you have a module object %0:

现在你就有了一个模块对象 %0:
pickle218

That provides a function called pickle219:

它提供了一个叫 pickle220 的函数:
pickle221

So that’s how you write modules in Python.

这就是在 Python 里写模块的办法。

The only problem with this example is that when you import the module it executes the test code at the bottom. Normally when you import a module, it defines new functions but it doesn’t execute them.

这个例子唯一的毛病在于:导入模块时,它会把底部的测试代码也执行一遍。通常导入模块只是定义新函数,并不会去执行它们。

Programs that will be imported as modules often use the following idiom:

打算被当模块导入的程序,常常用下面这个惯用法:
pickle222

pickle22 is a built-in variable that is set when the program starts. If the program is running as a script, pickle22 has the value pickle22; in that case, the test code is executed. Otherwise, if the module is being imported, the test code is skipped.

pickle22 是一个内建变量,程序启动时就被设置好。如果程序是作为脚本运行的,pickle22 的值就是 pickle22;此时测试代码会被执行。否则,如果是被当作模块导入,测试代码就会被跳过。

Exercise 5

习题 5

Type this example into a file named 'w'00 and run it as a script. Then run the Python interpreter and pickle230. What is the value of pickle23 when the module is being imported?

把这个例子敲进一个叫 'w'00 的文件,当作脚本运行。然后启动 Python 解释器并 pickle233。模块被导入时,pickle23 的值是什么?

Warning: If you import a module that has already been imported, Python does nothing. It does not re-read the file, even if it has changed.

注意:如果导入一个已经导入过的模块,Python 什么也不做。它不会重新读取文件,哪怕文件已经改了。

If you want to reload a module, you can use the built-in function pickle, but it can be tricky, so the safest thing to do is restart the interpreter and then import the module again.

想重新加载模块,可以用内建函数 pickle,但这事儿有点 tricky,所以最稳妥的还是重启解释器,再导入一次模块。

14.10 Debugging 14.10 调试

When you are reading and writing files, you might run into problems with whitespace. These errors can be hard to debug because spaces, tabs and newlines are normally invisible:

读写文件时,你可能会撞上空白字符带来的麻烦。这类错误很难调试,因为空格、制表符和换行通常都看不见:
pickle237

The built-in function 'w'0 can help. It takes any object as an argument and returns a string representation of the object. For strings, it represents whitespace characters with backslash sequences:

内建函数 'w'0 能帮上忙。它接收任意对象作参数,返回对象的字符串表示。对于字符串,它会用反斜杠序列把空白字符表示出来:
pickle240

This can be helpful for debugging.

这对调试很有帮助。

One other problem you might run into is that different systems use different characters to indicate the end of a line. Some systems use a newline, represented %0. Others use a return character, represented %0. Some use both. If you move files between different systems, these inconsistencies might cause problems.

另一个可能撞上的问题是:不同系统用不同的字符表示行尾。有的系统用换行符,记作 %0;有的用回车符,记作 %0;有的两者都用。若在不同系统间搬文件,这种不一致就可能惹麻烦。

For most systems, there are applications to convert from one format to another. You can find them (and read more about this issue) at pickle245. Or, of course, you could write one yourself.

针对大多数系统,都有现成工具能在两种格式间转换。你可以在 pickle246 找到它们(并了解更多)。当然,你自己写一个也行。

14.11 Glossary 14.11 术语表

persistent:
Pertaining to a program that runs indefinitely and keeps at least some of its data in permanent storage.
format operator:
An operator, %, that takes a format string and a tuple and generates a string that includes the elements of the tuple formatted as specified by the format string.
format string:
A string, used with the format operator, that contains format sequences.
format sequence:
A sequence of characters in a format string, like %0, that specifies how a value should be formatted.
text file:
A sequence of characters stored in permanent storage like a hard drive.
directory:
A named collection of files, also called a folder.
path:
A string that identifies a file.
relative path:
A path that starts from the current directory.
absolute path:
A path that starts from the topmost directory in the file system.
catch:
To prevent an exception from terminating a program using the 'w' and pickle statements.
database:
A file whose contents are organized like a dictionary with keys that correspond to values.
persistent 持久的:
指一个程序无限期运行,并把至少部分数据保存在永久存储中。
format operator 格式化运算符:
一个运算符 %,它接收格式化字符串和元组,生成包含元组中各元素、并按格式化字符串规定格式排布而成的字符串。
format string 格式化字符串:
与格式化运算符配合使用、含有格式化序列的字符串。
format sequence 格式化序列:
格式化字符串里的一串字符(如 %0),用于指定某个值该如何格式化。
text file 文本文件:
保存在硬盘等永久存储上的一串字符。
directory 目录:
有名字的文件集合,也叫文件夹。
path 路径:
标识一个文件的字符串。
relative path 相对路径:
从当前目录开始的路径。
absolute path 绝对路径:
从文件系统最顶层目录开始的路径。
catch 捕获(异常):
'w'pickle 语句防止异常把程序弄终止。
database 数据库:
内容组织得像字典一样的文件,键与值一一对应。

14.12 Exercises 14.12 习题

Exercise 6

习题 6

The pickle module provides methods for manipulating URLs and downloading information from the web. The following example downloads and prints a secret message from pickle256:

pickle 模块提供了处理 URL、从网上下载信息的方法。下面的例子从 pickle258 下载并打印出一条秘密消息:
pickle259

Run this code and follow the instructions you see there. Solution: pickle260.

运行这段代码,并照着屏幕上出现的指示去做。参考解答:pickle261。
1
'w'00 is deprecated now, which means we are supposed to stop using it and start using the pickle263 module. But for simple cases, I find pickle264 more complicated than necessary. So I am going to keep using 'w'00 until they take it away.
1
'w'00 现在已被弃用,意思是说我们该停用它、改用 pickle267 模块了。但在简单场景下,我觉得 pickle268 有点杀鸡用牛刀。所以在它被彻底拿走之前,我还会继续用 'w'00。