Exercise 15: Reading files
You've learned raw_input and argv, and these are the basics you'll start learning to read files. You may need to experiment more to understand how it works, so you should do the exercises carefully and examine the results carefully. The files need to be handled very carefully, and if not carefully, you may be able to break or empty the useful files. Lead to naught.
This exercise involves writing two of files. A normal ex15.py file, the other is Ex15_sample.txt, the second file is not a script, but a text file for your script to read. The following are the contents of the latter:
1 is stuff I typed into a file. 2 is really cool stuff. 3 and here.
All we have to do is put the file in our script "open" and print it out. However, it is not a good idea to write the file name ex15_sample.txt (hardcode) in the code, which should be the user input. If we encounter other files to deal with, writing dead filenames will cause you trouble. Our solution is to use argv and raw_input to get information from users to know which files should be processed.
1 fromSysImportargv2 3script, filename =argv4 5TXT =open (filename)6 7 Print "Here ' s your file%r:"%filename8 PrintTxt.read ()9 Ten Print "Type the filename again:" OneFile_again = Raw_input (">") A -Txt_again =Open (File_again) - the PrintTxt_again.read ()
View Code
There are some novelties in this script, let's go through it quickly:
The 1-3 lines of code use argv to get the file name, which you should already be familiar with. Next line 5th we see open this new command. Now, run pydocopen on the command line to read its description. You can see that it is similar to your own script, or raw_input command, which takes a parameter and returns a value that you can assign to a variable. This is the process of opening your file.
Line 7th We printed a small line, but on line 8th we saw something novel. We've used a function in txt . What you get from Open is a file , and the file itself supports some commands. The way it accepts commands is to use a period . (called dot or period), followed by your command, followed by parameters like open and raw_input . The difference is: when you say txt.read , you mean actually: "Hey txt! Execute your read command without any parameters! ”
The rest of the script is pretty much the same, but I'll leave the rest of the analysis as a plus-points exercise for you.
The results you should see
My script is called "Ex15_sample.txt", the following is the result of execution:
Bonus points Exercise
The difficulty of this section is a bit big, so you should try to do a good job of this plus points, and then continue the chapters later.
1. Use annotations on each line to explain the purpose of the line.
2. If you are unsure of the answer, ask someone, or search the Internet. Most of the time, just search for "python" plus what you're searching for to get the answer you want. For example, search for "Python open".
3. I use the word "command", but in fact their name is "function" and "method". Search the Internet for the meaning and difference between the two. It doesn't matter whether you see it or not, it's a normal thing to get lost in the knowledge of other programmers.
4. Delete the 10-15 lines using the Raw_input section and run the script again.
5. Just write this script with Raw_input, think of the way to get the file name better, and why.
6. Run Pydoc file to scroll down until you see the read () command (function/method). See a lot of other orders, you can find a few to try. No need to look at the commands that contain __ (two underscores), these are just rubbish.
7. Run Python again at the command line to open a file, this open and read method is also worth learning.
8. It is important to have your script execute close () against the TXT and txt_again variable and you need to close the file after processing it.
Stupid way to learn Python (15)