习题 17: 更多文件操作

阅读原文
现在让我们再学习几种文件操作。我们将编写一个 Python 脚本,将一个文件中的内容 拷贝到另外一个文件中。这个脚本很短,不过它会让你对于文件操作有更多的了解。

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

out_file = open(to_file, 'w')
out_file.write(indata)

print "Alright, all done."

out_file.close()
in_file.close()

你应该很快注意到了我们 import 了又一个很好用的命令 exists。这个命令 将文件名字符串作为参数,如果文件存在的话,它将返回 True,否则将返回 False。 在本书的下半部分,我们将使用这个函数做很多的事情,不过现在你应该学会 怎样通过 import 调用它。

通过使用 import ,你可以在自己代码中直接使用其他更厉害的(通常是这样,不过也不 尽然)程序员写的大量免费代码,这样你就不需要重写一遍了。

What You Should See

和你前面写的脚本一样,运行该脚本需要两个参数,一个是待拷贝的文件,一个是要拷贝至的 文件。如果我们使用以前的 test.txt 我们将看到如下的结果:



$ echo "This is a test file." > test.txt
$ cat test.txt
This is a test file.
$
$ python ex17.py test.txt new_file.txt
Copying from test.txt to new_file.txt
The input file is 21 bytes long
Does the output file exist? False
Ready, hit RETURN to continue, CTRL-C to abort.

Alright, all done.

该命令对于任何文件都应该是有效的。试试操作一些别的文件看看结果。不过小心别把你的重要 文件给弄坏了。

warning:: 你看到我用 cat 这个命令了吧?它只能在 Linux 和 OSX 下面使用,使用 Windows 的就只好跟你说声抱歉了。

Study Drills

  1. 再多读读和 import 相关的材料,将 python 运行起来,试试这一条命令。试着看看 自己能不能摸出点门道,当然了,即使弄不明白也没关系。
  2. 这个脚本 实在是 有点烦人。没必要在拷贝之前问一遍把,没必要在屏幕上输出那么多东西。 试着删掉脚本的一些功能,让它使用起来更加友好。
  3. 看看你能把这个脚本改多短,我可以把它写成一行。
  4. 我使用了一个叫 cat 的东西,这个古老的命令的用处是将两个文件“连接(concatenate)” 到一起,不过实际上它最大的用途是打印文件内容到屏幕上。你可以通过 man cat 命令了解到更多信息。
  5. 使用 Windows 的同学,你们可以给自己找一个 cat 的替代品。关于 man 的东西就 别想太多了,Windows 下没这个命令。
  6. 找出为什么你需要在代码中写 output.close()

Common Student Questions

Why is the 'w' in quotes?
That's a string. You've been using strings for a while now. Make sure you know what a string is
No way you can make this one line!
That ; depends ; on ; how ; you ; define ; one ; line ; of ; code.
Is it normal to feel like this exercise was really hard?
Yes, it is totally normal. Programming may not "click" for you until maybe even Exercise 36, or it might not until you finish the book and then make something with Python. Everyone is different, so just keep going and keep reviewing exercises that you had trouble with until it clicks. Be patient.
What does the len() function do?
It gets the length of the string that you pass to it then returns that as a number. Play with it.
When I try to make this script shorter I get an error when I close the files at the end.
You probably did something like this, indata = open(from_file).read(), which means you don't need to then do in_file.close() when you reach the end of the script. It should already be closed by Python once that one line runs.
I get a Syntax:EOL while scanning string literal error
You forgot to end a string properly with a quote. Go look at that line again.

Copyright (C) 2010 by
Author: Zed Shaw
Translator:Zander Wong

results matching ""

    No results matching ""