本文主要是介绍《笨办法学python》习题20:函数和文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
from sys import argvscript, input_file = argvdef print_all(f):
print f.read()
#readline() 方法用于从文件读取整行,包括 "\n" 字符。如果指定了一个非负数的参数,则返回指定大小的字节数,包括 "\n" 字符。
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:函数和文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!