An introduction to reading from a text file
Q1. A text file is one that is made up of pure ASCII characters. Create the following file in any text editor, such as Notepad in Windows. Do not use a word processor such as Word as these do not always create pure text files.
An old silent pond...
A frog jumps into the pond,
splash! Silence again.
A poem by Basho Matsuo (written in the 17th century).
Save the file as haiku.txt and save it in a folder called Python3.
This is the file that we will be using for the reading and writing examples.
Q2. When you read from and write to text files in Python, you always read and write only strings. If you want to read and write any other kind of data, you need to remember to use something called 'binary mode'. For the moment, you just need to know that you are always reading from and writing to files using strings, which is Python's default.
To use any file, you must do up to three things ('up to' because if you don't specify them, then common default values are used):
- firstly create what is called a file object.
- second, tell Python what you are going to do with the file - it's 'mode'
- thirdly, tell Python what encoding to use for the characters.
Open a new Python window and copy this program:
poem = open('haiku.txt','r',encoding='utf-8')
You must for the moment save it in the same folder as the Haiku poem.
Run the program and you should see the poem is displayed. What we did is to create a file object called poem and associated it with haiku.txt using open. Then we told Python that we will be reading from this file using the mode r. There are other modes. For example, you can tell Python you want to write to a file by using w, or add some string to the very end of an existing file by using a, or both read and write to the file using r+. If you miss out the mode, Python will assume you are reading from the file, mode r. Finally, we told Python how our characters are encoded by the computer. There are different ways to encode characters. We won't worry too much about this yet but we will use the most common one, and the one that Python will default to if we miss this argument out.
When you open a file object, it is important to remember to close it when you have finished with it. If you don't, it may cause unexpected problems when other parts of your program want to access the file. Open files also use up system resources, which is a waste if you don't need the files to be open anymore! You close our file object by using the name of the object and the close method i.e. poem.close()
Q3. Remove this: ,'r',encoding='utf-8' from the first line in your program so it looks like the following:
poem = open('haiku.txt')
Run it again, to see that your program still works because the default values have been used.
Q4. Now move your haiku poem to another folder using cut and paste in your File Manager. I moved mine to the 'My Videos' folder. Run the program again. What happened? You should have got an error message bacause Python couldn't find the file you wanted to use.
Q5. Python assumes that the file you want to access is in the same place as the saved Python file. If it is not, you must include the full path from the hard drive to the file. Don't worry. It's easy to do. In Windows, go to the File Manager and find the haiku poem. Right-click on it and go to Properties. Then highlight all of the location details and copy it. Now simply paste it in front on the fle name in your Python program and add a backslash. My program now looks like this:
# A program to demonstrate reading and writing
# from and to files.
poem = open('C:\Documents and Settings\Enjoy InternetWebcam\My Documents\My Videos\haiku.txt','r',encoding='utf-8')
Yours will be slightly different, depending upon what folders you have and where you stored your poem. Run the program and check it works. You now know how to store files anywhere you like, even on USB pen drives or SD cards. All you need to remember to do is find the full path to the file you want to use and then include it in your Python program, like we just did. Of course, to keep things simply, just put your program in the same place as the file you want to use! Put your file back in the same folder as your Python program now.
poem = open('haiku.txt','r',encoding='utf-8')
Q6. Using Notepad or a similar text editor (remember - not Word or another word processor) either write your own short haiku poem or copy a poem from the Internet. Save it in your Python folder.
Q7. Write a Python program to print out your poem.
Q8. Move your poem to a new folder.
Q9. Modify your program so it can find your file.
Q10. Move your poem to a USB pen drive or similar. Modify your program to include the full path from your external storage device to the file and check everything works as intended.