Lpthw stupid way to learn Python chapter 20

Source: Internet
Author: User
Tags rewind

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.