这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!
文中例题:
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, king 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)
运行结果:
习题答案:
1、
# -- coding: utf-8 --
# 从系统中引入argv
from sys import argv
# 引入一个参量,script为脚本名称
script, input_file = argv
# 打印脚本f
def print_all(f):
print f.read()
# 设置开始的偏移量,即从偏移位置开始运行
def rewind(f):
f.seek(1)
# 打印
def print_a_line(line_count, f):
print line_count, f.readline()
# 打开文件,将文件内容复制到current_file中
current_file = open(input_file)
print "First let's print the whole file:\n"
print_all(current_file)
print "Now let's rewind, king of like a tape."
rewind(current_file)
print "Let's print three lines:"
current_line = 1
# 传入参数current_line = 1 和文件 current_file
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)
2、就是一步一步带进去的。
4、如一中注释
转载至: http://www.runoob.com/python/file-seek.html
感谢菜鸟教程!
5、
count = 1
temp = 2
count += temp
print count