函数和文件:
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)
运行结果:
这里我还是用之前ex16创建的test.txt文本文档作为测试文件
> python .\ex20.py test.txt
First let's print the whole file:
Mary had a little lamb
It's fleece was white as snow
It was also tasty
Now let's rewind,kind of like a tape.
Let's print three lines:
1 Mary had a little lamb
2 It's fleece was white as snow
3 It was also tasty
加分习题:
Q1.为每一行加上注释,以便理解这一行的作用。
from sys import argv #引入sys模块
script,input_file=argv #解包参数,并赋值给变量script和input_file
def print_all(f): #定义函数print_all(打印整个文档)
print(f.read()) #让f执行read函数,读取文件并打印
def rewind(f): #定义函数rewind(重置指针到文件开头位置)
f.seek(0) #让f执行seek函数,将读写位置移动到文件开头
def print_a_line(line_count,f): #定义函数print_a_line,并给参数起名
print(line_count,f.readline()) #让f执行readline函数(只读取文本文件中的一行),并与参数line_count一起打印
current_file=open(input_file) #用open函数打开变量input_file,并赋值给变量current_file
print("First let's print the whole file:\n") #打印字符串并换行
print_all(current_file) #调用函数print_all,参数为变量current_file
print("Now let's rewind,kind of like a tape.") #打印字符串
rewind(current_file) #调用函数rewind,参数为变量current_file
print("Let's print three lines:") #打印字符串
current_line=1 #将整数1赋值给变量current_line
print_a_line(current_line,current_file) #调用函数print_a_line,参数为变量current_line和current_file
#变更行号,打印第二行,打印过一行之后,指针其实已经在第二行开头等着了。
current_line=current_line+1 #将current_line+1赋值给变量current_line
print_a_line(current_line,current_file) #调用函数print_a_line,参数为变量current_line和current_file
current_line=current_line+1 #将current_line+1赋值给变量current_line
print_a_line(current_line,current_file) #调用函数orint_a_line,参数为变量current_line和current_file
Q2.每次print_a_line运行时,你都传递了一个叫current_line的变量。在每次调用函数时,打印出current_line的值,跟踪一下它在print_a_line中是怎样变成 line_count的。
这里依旧引用CSDN上一位大神的解释:这里其实line_count要叫做位置参数,之所以调用时的参数current_line成为了函数定义时的line_count就是因为它们在定义与被调用时所处的位置是一样的。
如果我们把函数定义时两个参数的位置对调,并保持调用的顺序不变。或者,函数定义时不变,调用时对调。都会因为行号这个整数(int)没有readline()这个方法而导致错误发生。
Q3.找出脚本中每一个用到函数的地方。检查 def 一行,确认参数没有用错。
Q4.上网研究一下 file中的 seek函数是做什么用的。试着运行 pydoc file 看看能不能学到更多。然后试一下pydoc file.seek,看看seek是做什么用的。
习题16中已经讲过,seek(0)是将读写位置移动到文件开头。
要注意,seek()函数的处理对象是字节而非行,所以seek(0)只是转到文件的0字节,也就是第一个字节的位置。
Q5.研究一下 += 这个简写操作符的作用。重写这个脚本,在里边用一下这个操作符。
+=是赋值运算符,其含义是左边已存在的变量,与右边的值进行运算后,重新赋值给左边的变量。
这里可以把两个current_line=current_line+1换成current_line+=1
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+=1
print_a_line(current_line,current_file)
current_line+=1
print_a_line(current_line,current_file)
运行结果:
> python .\ex20_drills.py test.txt
First let's print the whole file:
Mary had a little lamb
It's fleece was white as snow
It was also tasty
Now let's rewind,kind of like a tape.
Let's print three lines:
1 Mary had a little lamb
2 It's fleece was white as snow
3 It was also tasty