python版本:3 若有错误,敬请指出 模块名称:测试.py
#加分习题20
from sys import argv#from模块名称import函数名称
script,input_file = argv#设置两个参数,命令行输入
current_file = open(input_file)#此句可在print("First..前一行,亦可在这(打开一个文件,创建一个 file 对象)
def print_all(f):
print(f.read())#读取文件内容
def rewind(f):
f.seek(0)#seek()方法标准格式是:seek(offset,whence=0)offset表需要移动偏移的字节数,更多看下 注
def print_a_line(line_count,f):
print(line_count,f.readline())#函数readline()读取文件的每一行
print("First let's print the whole file:\n")
print_all(current_file)#调用函数
print("Now let's rewind,kind of like tape.")
rewind(current_file)#调用函数
print("Let's print three lines:")
current_line = 1
print(current_line)
print_a_line(current_line,current_file)#调用函数
current_line = current_line + 1#1+1可改为2
print(current_line)
print_a_line(current_line,current_file)#调用函数
current_line = current_line + 1#1+1+1可改为3
print(current_line)
print_a_line(current_line,current_file)#调用函数
#注
#改为f.seek(3),运行结果截图名称:f.seek(3)
#f.seek(3)将当前的位置设定为相对当前位置的3的位置
#f.seek(0,1),抱歉不知道意思,因为运行结果看不懂,若有知道的朋友,望告之
#f.seek(1,1),不懂
#1
#如上注释
#2
#添加代码print(current_line),运行结果截图名称:#2
#3略
#4
#如上注释及注
#5
#current_line = current_line + 1等价于current_line += 1
截图名称:习题
截图名称:f.seek(3)
截图名称:#2