习题 20: 函数和文件

阅读原文

回忆一下函数的要点,然后一边做这节练习,一边注意一下函数和文件是如何 在一起协作发挥作用的。

from sys import argv

script, input_file = argv

def print_all(f):
    print f.read()

def rewind(f):
    f.seek(0)

def print_a_line(line_count, f):
    print line_count, f.readline()

current_file = open(input_file)

print "First let's print the whole file:\n"

print_all(current_file)

print "Now let's rewind, kind of like a tape."

rewind(current_file)

print "Let's print three lines:"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

特别注意一下,每次运行 print_a_line 时,我们是怎样传递当前的行号 信息的。

What You Should See

$ python ex20.py test.txt
First let's print the whole file:

This is line 1
This is line 2
This is line 3

Now let's rewind, kind of like a tape.
Let's print three lines:
1 This is line 1

2 This is line 2

3 This is line 3

Study Drills

  1. 通读脚本,在每行之前加上注解,以理解脚本里发生的事情。
  2. 每次 print_a_line 运行时,你都传递了一个叫 current_line 的变量。 在每次调用函数时,打印出 current_line 的至,跟踪一下它在 print_a_line 中是怎样变成 line_count 的。
  3. 找出脚本中每一个用到函数的地方。检查 def 一行,确认参数没有用错。
  4. 上网研究一下 file 中的 seek 函数是做什么用的。试着运行 pydoc file 看看能不能学到更多。
  5. 研究一下 += 这个简写操作符的作用,写一个脚本,把这个操作符用在里边试一下。

Common Student Questions

What is f in the print_all and other functions?
The f is a variable just like you had in other functions in Exercise 18, except this time it's a file. A file in Python is kind of like an old tape drive on a mainframe, or maybe a DVD player. It has a "read head," and you can "seek" this read head around the file to positions, then work with it there. Each time you do f.seek(0) you're moving to the start of the file. Each time you do f.readline() you're reading a line from the file, and moving the read head to right after the \n that ends that line. This will be explained more as you go on.
Why does seek(0) not set the current_line to 0?
First, the seek() function is dealing in Bytes, not lines. The code seek(0) moves the file to the 0 byte (first byte) in the file. Second, current_line is just a variable and has no real connection to the file at all. We are manually incrementing it.
What is +=?
You know how in English I can rewrite "it is" as "it's"? Or I can rewrite "you are" as "you're"? In English this is called a contraction, and this is kind of like a contraction for the two operations = and +. That means x = x + y is the same as x += y.
How does readline() know where each line is?
Inside readline() is code that scans each byte of the file until it finds a \n character, then stops reading the file to return what it found so far. The file f is responsible for maintaining the current position in the file after each readline() call, so that it will keep reading each line.
Why are there empty lines between the lines in the file?
The readline() function returns the \n that's in the file at the end of that line. Add a , at the end of your print function calls to avoid adding double \n to every line.

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

results matching ""

    No results matching ""