Stupid way to learn Python: functions and files

Source: Internet
Author: User
Tags rewind

 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

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.