CS105 Study Guide

Unit 7: File Handling

7a. Use file handling and file handling modes to read and write to text files

  • What are the basic steps required for handling a file?
  • What Python methods accomplish these basic steps?
  • What is the Python syntax for reading from and writing to a file?

To perform file handling, regardless of the programming language, there are some basic steps that must take place. First, a file must be opened by referring to the correct path, file name, and file extension. Once opened, the desired file handling mode (such as read or write) must be specified. Finally, once a program is finished accessing a given file, it must be closed.

These steps translate almost directly into the method calls that implement them. The open method opens a file along with choosing the file handling mode. Read ("r") and write ("w") are the most basic ways of performing desired operations. Finally, the close method closes the file.

A basic instruction sequence for a file read, assuming filename.txt exists, would look like:


If the file handling mode is omitted, the default mode for the open method is read ('r').

A basic instruction sequence for writing text to a file, would look like:


Both of these examples highlight how to open a file, how to read from or write to a file, and how to close a file. All file handling operations utilize this basic structure.

To review, see Syntax and Usage.


7b. Write programs that use file handling modes, such as reading from, writing to, appending, and creating files

  • What is the syntax for creating a file?
  • What are the main differences between writing and appending?\
  • What are some basic instruction sequences useful for file handling?

In addition to writing to a file, the ability to append data to a file must also be a possibility. It is important to understand the basic similarities and differences between writing and appending.

  • If a file does not exist, the open method will
    • Generate a FileNotFoundError exception if read mode ('r') is used
    • Create the file if either write ('w') or append ('a') mode is used

So, both the write and append modes have the ability to create a file. Once created, the file is empty and contains no data. While the newly created file is still opened, either mode 'w' or 'a' will allow for writing multiple instances of data to the file and allow multiple calls of the write method.

When dealing with files that already exist, then some care must be taken.

  • If a file does exist, the open method will
    • Create an empty file if 'w' is used, thus, obliterating any previous contents contained in the file
    • Allow for appending data to the file (if the 'a' file handling mode is used) using the write method.

For example, if filename.txt does not exist, then




are equivalent. On the other hand, if filename.txt does exist, then


will create a new empty file while


will not destroy the existing contents and data will be appended using the write method.

To review, see Syntax and Usage.


7c. Write programs that using file handling methods

  • Name some applications where file handling can be useful.
  • What methods can be helpful when parsing data read from a file?
  • How can loops be used to help read file data?

 File handling is extremely useful for reading large amounts of data stored in a file. Your word processor and spreadsheet application continually perform file handling in order to save and open documents. When reading large amounts of data, it may become necessary to read or write data a single line at a time rather than using one single read or write. For example, using a for loop such as:

myfile = open('filename.txt')
for line in myfile:

will read and print out each line contained within the file 'filename.txt'. The readline method is also useful for reading single lines as well. Finally, if a line of text is read that has multiple values that need to be converted to numerical data, the split method is indispensable. For example, if a file source.txt contains the data:

2 4
3 5
4 6

the following code

myfile = open('source.txt')
for line in myfile:
    line = line.rstrip()
    a=line.split(' ')

would generate the output


In this example, the split method splits each line using the whitespace character ' '.

To review, see Syntax and Usage.


7d. Apply file handling to the data analysis and visualization programs written in Units 3-6

  • How can file handling be useful for data visualization?
  • What file handling methods can be applied when handling large amounts of data?
  • What basic Python data structure can be used to house data read from a file?

Data visualization is yet another application where file handling can be important when the amount of data to be rendered is large and keyboard entry is not a possibility. Depending on the ordering of how the data is stored, methods such as readline and split can be very useful for parsing data into lists and then can be plotted using methods from matplotlib.

So-called 'space-delimited data' is configured assuming each value on a given line is separated by a blank space ' '. This is partially true for data files with the extension '.csv' (commonly used for storing spreadsheet data). For example,

10 33
22 64
34 15
39 12
74 95

is an example of space-delimited data. Assuming matplotlib is imported, the following set of instructions could be used to create lists from this data stored in a file 'filename.txt' that could be plotted:

x=[ ]
y=[ ]
myfile = open('filename.txt')
for line in myfile:
    line = line.rstrip()
    z=line.split(' ')

To review, see Data Visualization from a Data File.


Unit 7 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • read
  • write
  • append
  • split method
  • matplotlib