File I/O
Andrew S. Fitz Gibbon
UW CSE 160
Winter 2024
1
File Input and Output
(What even is a file?)
2
Files store information�when a program is not running
Important operations:
3
Files and filenames
4
Two types of filenames
An Absolute filename gives a specific and complete location in the file system:
A Relative filename gives a location relative to the current working directory:
5
Examples
Linux/Mac: These could all refer to the same file:
"/home/asfg/class/160/homework3/images/Husky.png"
"homework3/images/Husky.png"
"images/Husky.png"
"Husky.png“
Windows: These could all refer to the same file:
"C:\Users\asfg\My Documents\class\160\homework3\images\Husky.png"� "homework3\images\Husky.png"
"images\Husky.png"
"Husky.png"
6
Aside: “Current Working Directory” in Python
Current Working Directory (sometimes also called the "Present" working directory) - the directory from which you ran Python
To determine it from a Python program:
import os
print("The current working directory is", os.getcwd())
Might print:
'/Users/johndoe/Documents'
7
os stands for “operating system”
Opening a file in python
To open a file for reading:
# Open takes a filename and returns a file object.
# This fails if the file cannot be found & opened.
myfile = open("datafile.dat")
myfile = open("datafile.dat", "r")
To open a file for writing:
# Will create datafile.dat if it does not already �# exist, if datafile.dat already exists, then it�# will be OVERWRITTEN
myfile = open("datafile.dat", "w")
# If datafile.dat already exists, then we will�# append what we write to the end of that file
myfile = open("datafile.dat", "a")
8
By default, file is opened for reading
Adding "r" makes read-only explicit.
Adding "a" opens for appending.
Reading a file in python
# Open takes a filename and returns a file object.
# This fails if the file cannot be found & opened.
myfile = open("datafile.dat")
# Approach 1: Process one line at a time
for line_of_text in myfile:
# process line_of_text
# Approach 2: Process entire file at once
all_data_as_a_big_string = myfile.read()
myfile.close() # close the file when done reading
Assumption: file is a sequence of lines
Where does Python expect to find this file (note the relative pathname)?
9
Simple Reading a file Example
# Reads in file one line at a time and
# prints the contents of the file.
in_file = "student_info.txt"
myfile = open(in_file)
for line_of_text in myfile:
print(line_of_text)
myfile.close()
10
Reading a file Example
# Count the number of words in a text file
in_file = "thesis.txt"
myfile = open(in_file)
num_words = 0
for line_of_text in myfile:
word_list = line_of_text.split()
num_words += len(word_list)
myfile.close()
print("Total words in file: ", num_words)
11
Reading a file multiple times
You can iterate over a list as many times as you like:
mylist = [ 3, 1, 4, 1, 5, 9 ]
for elt in mylist:
… process elt
for elt in mylist:
… process elt
Iterating over a file uses it up:
myfile = open("datafile.dat")
for line_of_text in myfile:
… process line_of_text
for line_of_text in myfile:
… process line_of_text
12
This loop body will never be executed!
In general, try to avoid reading a file more than one time. Reading files is slow.
Reading a file multiple times
How to read a file multiple times?
Solution 1: Read into a list, then iterate over it
myfile = open("datafile.dat")
mylines = []
for line_of_text in myfile:
mylines.append(line_of_text)
for line_of_text in mylines:
… process line_of_text
for line_of_text in mylines:
… process line_of_text
Solution 2: Re-create the file object (slower, but a better choice if the file does not fit in memory)
myfile = open("datafile.dat")
for line_of_text in myfile:
… process line_of_text
myfile = open("datafile.dat")
for line_of_text in myfile:
… process line_of_text
13
Writing to a file in python
# Replaces any existing file of this name
myfile = open("output.dat", "w")
# Similar to printing output
myfile.write("a bunch of data")
# but you must add newline if desired, unlike print
myfile.write("a line of text\n")
# and the argument must be a string
myfile.write(4)
myfile.write(str(4))
myfile.close()
14
open for Writing�(give no argument, or
"r", for Reading)
“\n” means end of line (Newline)
Incorrect; results in:
TypeError: expected a character buffer object
Similar to if you tried to do print(4)
Correct. Argument must be a string
close when done with all writing
Next thing written will be on this same line.
# Count the number of words in a text file and
# make a list of all the words in the file
num_words = 0
word_list = []
silly_file = open("silly.txt", "r")
for line in silly_file:
print(line, end="")
# what should come next? (Hint: use split())
silly_file.close()
print("Total words in file: ", num_words)
15
16
num_words = 0
word_list = []
silly_file = open("silly.txt", "r")
for line in silly_file:
new_words = line.split()
word_list.extend(new_words)
num_words = num_words + len(new_words)
silly_file.close()
print("Total word count:", num_words)
print(word_list)
This is a silly file.
Here is some more silly text.
And even another silly line.
The fourth silly line.
17