习题 15: 读取文件
你已经学过了 raw_input
和 argv
,这些是你开始学习读取文件的必备基础。你
可能需要多多实验才能明白它的工作原理,所以你要细心做练习,并且仔细检查结果。处理
文件需要非常仔细,如果不仔细的话,你可能会吧有用的文件弄坏或者清空。导致前功尽弃。
这节练习涉及到写两个文件。一个正常的 ex15.py
文件,另外一个是 ex15_sample.txt
,
第二个文件并不是脚本,而是供你的脚本读取的文本文件。以下是后者的内容:
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
我们要做的是把该文件用我们的脚本“打开(open)”,然后打印出来。然而把文件名
ex15_sample.txt
写死(hardcode)在代码中不是一个好主意,这些信息应该是用户
输入的才对。如果我们碰到其他文件要处理,写死的文件名就会给你带来麻烦了。我们的
解决方案是使用 argv
和 raw_input
来从用户获取信息,从而知道哪些文件该
被处理。
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
print "Type the filename again:"
file_again = raw_input("> ")
txt_again = open(file_again)
print txt_again.read()
这个脚本中有一些新奇的玩意,我们来快速地过一遍:
代码的 1-3 行使用 argv
来获取文件名,这个你应该已经熟悉了。接下来第 5 行我\
们看到 open
这个新命令。现在请在命令行运行 pydoc open
来读读它的说明。\
你可以看到它和你自己的脚本、或者 raw_input
命令类似,它会接受一个参数,并\
且返回一个值,你可以将这个值赋予一个变量。这就是你打开文件的过程。
第 7 行我们打印了一小行,但在第 8 行我们看到了新奇的东西。我们在 txt
上调\
用了一个函数。你从 open 获得的东西是一个 file
(文件),文件本身也支持一些\
命令。它接受命令的方式是使用句点 .
(英文称作 dot 或者 period),紧跟着你的\
命令,然后是类似 open
和 raw_input
一样的参数。不同点是:当你说
txt.read
时,你的意思其实是:“嘿 txt!执行你的 read 命令,无需任何参数!”
脚本剩下的部分基本差不多,不过我就把剩下的分析作为加分习题留给你自己了。
What You Should See
我的脚本叫 "ex15_sample.txt",以下是执行结果:
$ python ex15.py ex15_sample.txt
Here's your file 'ex15_sample.txt':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Type the filename again:
> ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Study Drills
这节的难度跨越有点大,所以你要尽量做好这节加分习题,然后再继续后面的章节。
- 在每一行的上面用注解说明这一行的用途。
- 如果你不确定答案,就问别人,或者上网搜索。大部分时候,只要搜索 "python" 加上\ 你要搜的东西就能得到你要的答案。比如搜索一下“python open”。
- 我使用了“命令”这个词,不过实际上它们的名字是“函数(function)”和“方法(method)。\ 上网搜索一下这两者的意义和区别。看不明白也没关系,迷失在别的程序员的知识海洋\ 里是很正常的一件事情。
- 删掉 10-15 行使用到
raw_input
的部分,再运行一遍脚本。 - 只是用
raw_input
写这个脚本,想想那种得到文件名称的方法更好,以及为什么。 - 运行
pydoc file
向下滚动直到看见read()
命令(函数/方法)。看到很多\ 别的命令了吧,你可以找几条试试看。不需要看那些包含__
(两个下划线)的\ 命令,这些只是垃圾而已。 - 再次运行
python
在命令行下使用open
打开一个文件,这种 open 和 read 的方法也值得你一学。 - 让你的脚本针对
txt
andtxt_again
变量执行一下close()
,处理完\ 文件后你需要将其关闭,这是很重要的一点。
Common Student Questions
Does txt = open(filename) return the contents of the file? |
---|
No, it doesn't. It actually makes something called a "file object." You can think of a file like an old tape drive that you saw on mainframe computers in the 1950s, or even like a DVD player from today. You can move around inside them, and then "read" them, but the DVD player is not the DVD the same way the file object is not the file's contents. |
I can't type code into my Terminal/PowerShell like you say in Study Drill 7. |
First thing, from the command line just type python and press Enter. Now you are in python as we've done a few other times. Then you can type in code and Python will run it in little pieces. Play with that. To get out of it type quit() and hit Enter. |
Why is there no error when we open the file twice? |
Python will not restrict you from opening a file more than once and sometimes this is necessary. |
What does from sys import argv mean? |
For now just understand that sys is a package, and this phrase just says to get the argv feature from that package. You'll learn more about these later. |
I put the name of the file in as script, ex15_sample.txt = argv but it doesn't work. |
No, that's not how you do it. Make the code exactly like mine, then run it from the command line the exact same way I do. You don't put the names of files in, you let Python put the name in. |
Copyright (C) 2010 by
Author: Zed Shaw
Translator:Zander Wong