Learners will be able to:
- Use Python to write to a text file
- Use Python to read from a text file
- Adapt the code to select a certain line from the text document
Introduction:
- Python can be used to open a text file and write data to the file and then save it. You can also code it to open a file, read the contents and bring this data back into Python
- The code to open a text file is file = open("test.txt", "w")
- Open means open the text file, the next part is the name of the file to open, test.txt and lastly the w means open it in write mode.
- Add the data using the line file.write(file_text) where file_text is a variable that holds the data to be added to the text file.
- Then you add the data and finally close the text file with file.close()
- Look at the example below and add the name Frank to the text file.
Adding More Than One Item:
- Pretty awesome! No run the program three times and add three new names. What happens?
- Create a loop to keep the program asking you to add new data, note the +'\n' on line 9, what do you think that does?
- Add three new names to the list and check the text file
- Talk to another student about how to solve the loop issue.
- What does +'\n'do?
A Cleaner Way?
- A cleaner way to enter the data may be to create a list and get the user to enter in all the data first, then to open the text file and parse through the list adding each line of data.
- Look at the example below, run it and create an algorithm of the process.
Now That I Can Write I Need To Read!
- Next trick is to read data back from the text file.
- This is similar to the write code except that .read is used
- Look at the code below, look at the text file called Grace.
- Run the code and enter the Text file name, what does it do?
Read a Single line of Text From a File:
- So now you can read from a file but you may only want to read one line.
- Look at the code below and run it, what does it do?
- Which line is printed out?
- In the program, what is the code responsible for printing out one line?
- Each of the letters has a 'position' value, the first letter is 0, the second is 1 and so on. To print the letter you add the number to the print line, for example print line[4:6]
- Add this edit in, run the program, what does it do, which letters are printed?
- Use the same skills to print line 4 of the list, the output is "RTH"
Finding the Data That You Want:
- Grace Hopper has found some bugs in the programs, again!
- Sometimes you may want to find a particular word or phrase. This requires reading all the data and placing it into a variable which can then be split. Then a loop can be used to check each data entry to see if it matches a required number.
- Run the program, what does it do?
- Change line 19 to find the code "JDHJFD",
- Add a print statement to print the code that it finds
- Add a print statement to display a message if the code is not the code Grace is looking for.
- Can you adapt the code to find two or more of the errors?
Plenary:
- What have you learnt today?
- What was challenging? How did you overcome this?
- Ask another student what they have learnt?
- What do you want to do next?