很简单的代码。出自《笨办法学python》fromsysimportargvscript,input_file=argvdefprint_all(f):printf.read()defrewind(f):f.seek(0)defprint_a_line(line_count,f):printline_coun...
很简单的代码。出自《笨办法学python》
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)
出自《笨办法学python》习题20 主要有两个问题
1、 f.read() 应该是读取 文件f的内容。但是代码并没有把文件赋值给f。那么他是怎么工作的?
2、def 是定义一个函数,应该怎么理解? 这段代码是不是把print_all(f)和 print f.read()
定义了?还是 print f.read() 定义为函数,这个函数的名字叫print_all(f)?
展开