笨办法学python加分习题20

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值