Stupid way to learn Python (16)

Source: Internet
Author: User

Exercise 16: Reading and writing files

If you do a bonus exercise on the previous exercise, you should have already learned about the various file-related commands (methods/functions). The commands you should remember are as follows:

    • close– close the file. Save with your editor's file : one meaning.
    • read– reads the contents of the file. You can assign the result to a variable.
    • Readline– reads a line in a text file.
    • truncate– empty the file, use this command with care.
    • Write (Stuff) – writes stuff to the file.

This is the important command you should know now. Some commands need to accept parameters, which is not important to us. You just have to remember the use of write . write needs to receive a string as a parameter to write the string to the file.

Let's use these commands to make a simple text editor:

1  fromSysImportargv2 3script, filename =argv4 5 Print "We ' re going to erase%r."%filename6 Print "If you don ' t want that, hit Ctrl-c (^c)."7 Print "If You do want this, hit RETURN."8 9Raw_input ("?")Ten  One Print "Opening the file ..." Atarget = open (filename,'W') -  - Print "truncating the file. goodbye!" the target.truncate () -  - Print "Now I ' m going-to-ask for three lines." -  +line1 = Raw_input ("Line 1:") -Line2 = Raw_input ("Line 2:") +Line3 = Raw_input ("Line 3:") A  at Print "I ' m going to write these to the file." -  - target.write (line1) -Target.write ("\ n") - target.write (line2) -Target.write ("\ n") in target.write (line3) -Target.write ("\ n") to  + Print "and finally, we close it." -Target.close ()
View Code

This file is big enough, probably the largest file you've ever typed. So take it slow, check it out, and let it run. One small trick is that you can get your script to run in part. Write 1-8 lines First, let it run, run 5 more lines, then run a few more lines, and so on, until the entire script is up and running.

The results you should see

You will see two things, the same as the output of your new script:

Next Open your new file (mine is test.txt ) Check the contents of the inside, OK?

Bonus points Exercise
    1. If you feel that you are not understanding, use our old way, add annotations before each line, and clarify your ideas for yourself. Even if you can't clear your mind, you can also know exactly where you really didn't understand.
    2. Write a script similar to the one in the previous exercise and read the file you just created, using Read and argv .
    3. There are too many places to repeat in the file. Try to print line1, line2, line3 with a target.write () , you can use strings, format characters, and escape characters.
    4. Find out why we need to give open multiple a ' W ' parameter. Tip: Open writes to a file is safe first, so you can write to it only after you specifically specify it.
Exercise Exercises

1.

Dumb Way to learn Python (16)

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.