file = input("请输入要打开的文件:") demand = input("请输入必要体现的行数:") f = open(file) demand_1 = demand.split(":")[0] demand_2 = demand.split(":")[1] def begin(): print("文件" + file + "从开始到第" + str(demand_2) + "行的内容如下:") for i in range(int(demand_2)): print(f.readline()) f.close() def middle(): print("文件" + file + "从" + str(demand_1) + "到第" + str(demand_2) + "行的内容如下:") for i in range(int(demand_1)-1): f.readline() num = int(demand_2) - int(demand_1)+ 1 for i in range(num): print(f.readline()) f.close() def end(): print("文件" + file + "从" + str(demand_1) + "行到末了的内容如下:") for i in range(int(demand_1)-1): f.readline() num = len(list(f)) - int(demand_1) for i in range(num): print(f.readline()) f.close() if demand_1 == "": begin() elif demand_2 == "": end() else: middle() 我在输入界面输入“1:”大概“2:”的时间,可以正常进入end函数,但是打印不出东西,叨教这是那里有标题,谢谢 登录/注册后可看大图 最佳答案月排行榜 /总排行榜geen4 天前 num = len(list(f)) - int(demand_1) #缘故原由:这句里的 list(f) 把 f 的指针指到了末了,下面的for循环的f已为空。跳转到最佳答案楼层 |