This section describes the integrated operation of functions and files.
were carried out separately. 1, read out all the contents of the file, 2, reset the file to the beginning of the file. 3, print a line.
I made a small improvement in this section, set up a global variable, record which line should be entered at the moment, and if so, I'll add one to the number.
The code section is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*- fromSysImportArgvscript, Input_file=argvdefPrint_all (f):PrintF.read ()defRewind (f):GlobalLine_no f.seek (0, 0) line_no= 1defPrint_a_line (line_count,f):Globalline_no Line_Count=Line_noPrintline_count,f.readline () line_no+ = 1Current_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:" forIinchXrange (3): Print_a_line (line_no,current_file)
Lpthw stupid way to learn Python chapter 20