首页 > 代码库 > ex20.py

ex20.py

 1 # -*- coding: utf-8 -*-
 2 from sys import argv
 3 
 4 script, input_file = argv
 5 
 6 def print_all(f):
 7     print (f.read())
 8     
 9 def rewind(f):
10     f.seek(0)  
11 #seek那个函数不返回值,你print淡然显示为None了,file.seek(0)是重新定位在文件的第0位及开始位置
12 #file.seek(3) 定位到第3个
13 
14 def print_a_line(line_count, f):
15     print (line_count, f.readline())  #如果readline 中加入数值,看看什么含义?
16     
17 current_file = open(input_file)
18 
19 print ("Frist let‘s print the whole file:\n")
20 
21 print_all(current_file)
22 
23 print ("Now let‘s rewind, kind of like a tape.")
24 
25 rewind(current_file)
26 
27 print("let‘s print three lines:")
28 
29 current_line = 1
30 print_a_line(current_line,current_file),   #X += Y 和X = X + Y 意思相同
31 
32 current_file.seek(20)   #注意到这个作用,它是将readline()中的读取后光标的位置重新定位到seek()中字符的位置
33 
34 current_line = current_line + 1
35 print_a_line(current_line,current_file),
36 
37 current_line = current_line + 1
38 print_a_line(current_line, current_file)

 

ex20.py