26
E X E R C I S E # 8 : R E A D W R I T E F I L E
File I/O, or file input/output, allows your programs to read and write data to files on the hard
drive. This exercise covers just the basics of writing text to a file with Python code and then reading
the text from the file you just created. File I/O is an important technique because it allows your
programs to save data so that work isn’t lost when the program closes.
Exercise Description
You will write three functions for this exercise. First, write a writeToFile() function with
two parameters for the filename of the file and the text to write into the file. Second, write an
appendToFile()
function, which is identical to writeToFile() except that the file opens in
append mode instead of write mode. Finally, write a readFromFile() function with one parameter
for the filename to open. This function returns the full text contents of the file as a string.
These Python instructions should generate the file and the assert statement checks that the
content is correct:
writeToFile('greet.txt', 'Hello!\n')
appendToFile('greet.txt', 'Goodbye!\n')
assert readFromFile('greet.txt') == 'Hello!\nGoodbye!\n'
This code writes the text 'Hello!\n' and 'Goodbye!\n' to a file named
greet.txt, then reads
in this file’s content to verify it is correct. You can delete the
greet.txt file after running these
instructions program.
Try to write a solution based on the information in this description. If you still have trouble
solving this exercise, read the
Solution Design and
Special Cases and Gotchas sections for
additional hints.
Prerequisite concepts: text file reading and writing
Dostları ilə paylaş: