fromSysImportArgvscript,input_file=argvdefPrint_all (f):PrintF.read ()defRewind (f): F.seek (0)defPrint_a_line (line_count,f):Printline_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, kind of like a tape."Rewind (Current_file)Print"Let ' s print three lines:"Current_line=1print_a_line (current_line,current_file) current_line=current_line+1print_a_line (current_line,current_file) current_line=current_line+1print_a_line (current_line,current_file)
FAQ's Q2: Why are there 3 blank lines in the file?
The function ReadLine () returns a line of file contents ending with \ n, adding a comma ', ' at the end of your call to the print function, to avoid adding two newline characters to each line \ n
def Print_a_line (line_count,f): print line_count,f.readline (),
Stupid way to learn Python: functions and files