码迷,mamicode.com
首页 > 其他好文 > 详细

ex20.py

时间:2016-11-20 13:44:05      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:nes   lin   code   file   作用   imp   from   数值   div   

 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

标签:nes   lin   code   file   作用   imp   from   数值   div   

原文地址:http://www.cnblogs.com/jiangzhipeng/p/6082361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!