Use Python to read and write text files and write simple text editors

Source: Internet
Author: User
Learning Raw_input and argv is a prerequisite for learning to read files, and you may not fully understand this exercise, so study and examine it carefully. If you are not serious, it is easy to delete some useful files.

This exercise consists of two files, one running the file ex15.py, the other being ex15_sample.txt. The second file is not a script file, only contains some text, as follows:

This is the stuff I typed into a file. It is really cool stuff. Lots and Lots of fun to has in here.

All we have to do is open this file and print the contents of the file, we don't write the dead file name in the code, because if we want to read other files, we need to re-modify the code, the solution to this problem is to use argv and raw_input.

From sys import argv   script, filename = argv   txt = open (filename)   print "Here ' s your file%r:"% filename prin T Txt.read ()   print "Type the filename again:" File_again = Raw_input (">")   txt_again = open (File_again)   

The above code does some interesting things, let's break it down quickly:

1-3 rows use argv to get the file name. Line 5th uses the Open command, now use Pydoc open to see the introduction of this command.

Line 7th prints a line of information, but line 8th has something new. We have used a method in the TXT upgrade. We get a file through the open method, which has some methods that we can call. The way to use these methods is to add a. (point) to the file, such as Txt.read (), as if saying, "Hey, execute the Read command, no parameters!" ”

The rest of us are analyzing it in the bonus points exercise.

Run results

root@he-desktop:~/mystuff# python ex15.py ex15_sample.txt

Here's your file ' Ex15_sample.txt ': This is the stuff I typed into a file. It is really cool stuff. Lots and Lots of fun to has in here. Type the filename again:> ex15_sample.txtthis is stuff I typed into a file. It is really cool stuff. Lots and Lots of fun to has in here.


Commands for the following files are more commonly used:

    • Close-Closes the file, equivalent to the File->save in the editor
    • Read-Reads the contents of a file and assigns it to a variable
    • ReadLine--Reads a line of content
    • Truncate--Clear the file, use this command with care
    • Write (stuff)--Writes the file.

These are important commands that you should know, and only write needs to provide parameters.

Let's use these commands to implement a simple text editor.

From sys import argv   script, filename = argv   print "We" re going to erase%r. "% filename print" If you don ' t want That, hit Ctrl-c (^c). "Print" If you do want that, hot RETURN. "   Raw_input ("?")   Print "Opening the file ..." target = open (filename, ' W ')   print "truncating the file. goodbye!! "Target.truncate ()   print" Now I ' m going to ask you for three lines. "   line1 = Raw_input ("Line 1:") line2 = Raw_input ("Line 2:") Line3 = Raw_input ("Line 3:")   print "I-m going to write th ESE to the   file. Target.write (line1) target.write ("\ n") target.write (line2) target.write ("\ n") target.write (line3) target.write ("\ n" )   

This program is relatively long, so take it slow and let it run. One way to do this is to write a few lines first, run it, and then write a few more lines until you can run them.

Run results
You will see two things, one is the output of the program:

root@he-desktop:~/mystuff# python ex16.py test.txt

We ' re going to erase ' test.txt '. If you don ' t want that, hit Ctrl-c (^c). If You do want this, hot RETURN.? Opening the file ... Truncating the file. goodbye!! Now I ' m going to ask you for three lines.line 1:hi!line 2:welcome to my blog!line 3:thank you! I ' m going to write these to the file. And finally, we close it.

There is your newly created file, open to see it.

  • 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.