This article mainly introduces how to use Python to read and write text files and a simple text editor. You can use a simple Python code to perform operations such as reading and clearing and creating text files in the editor, you can refer to raw_input and argv as the prerequisites for learning to read files. You may not fully understand this exercise, so study and check carefully. If you are not careful, it is easy to delete some useful files.
This exercise package contains two files, one is the runtime file ex15.py, and the other is ex15_sample.txt. The second file is not a script file and only contains some text, as shown below:
This is stuff I typed into a file.It is really cool stuff.Lots and lots of fun to have in here.
All we need to do is open the file and print the file content. We do not write the dead file name in the code, because if we want to read other files, we need to modify the code again, to solve this problem, use argv and raw_input.
from sys import argv script, filename = argv txt = open(filename) print "Here's your file %r:" % filename print txt.read() print "Type the filename again:" file_again = raw_input("> ") txt_again = open(file_again) print txt_again.read()
The code above has done some interesting things, so that we can quickly break it down:
1-3 rows use argv to get the file name. Use the open command in line 5th. Now use pydoc open to see the introduction of this command.
Line 3 prints a row of information, but line 3 has something new. We call a method in txt. We get a file through the open method, which has some methods that we can call. The method to use these methods is to add a. (DOT) after the file, such as txt. read (), as if to say: "Hey, execute the read command, there is no parameter !"
Analyze the remaining parts in the extra points exercise.
Running result
root@he-desktop:~/mystuff# python ex15.py ex15_sample.txt
Here's your file 'ex15_sample.txt':This is stuff I typed into a file.It is really cool stuff.Lots and lots of fun to have 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 have in here.
The commands for the following files are commonly used:
- Close -- close the File, which is equivalent to File-> Save in the editor.
- Read-read File Content is allocated to a variable
- Readline -- read a row of content
- Truncate -- clear the file and use this command with caution
- Write (stuff) -- write a file.
These are important commands you should know. 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 these to the file." target.write(line1) target.write("\n") target.write(line2) target.write("\n") target.write(line3) target.write("\n") print "And finally, we close it." target.close()
This program is relatively long, so you can run it slowly. There is a way to write a few lines first, run it, you can run it and then write a few lines until all can run.
Running result
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 that, 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 also your new file. Open it.