Learner objectives for this lesson
while
and for
loops to read in and write data to filesMany applications require storing and retrieving data outside of a program. Think of the many different applications you regularly use, do they utilize information that has been saved in some way?
if balance >= purchase
)A simple way to store data is in a text file, such as this simple text file, transactions.txt, that stores an individual's credit card transaction history. Each line in the file represents a transaction price.
To process data in a file, we typically take the following approach:
Before we can read from a file or write to a file, we first need to open the file and get a file object (AKA handle). We do this with the built-in function open()
:
# in_file is our variable connecting our program to transactions.txt
# transactions.txt is a file I have in the *same folder* as this running Python file
in_file = open("transactions.txt", "r")
The first argument to open()
is a string representing the path to the file and the second argument is the file opening mode:
You can read more about modes here.
open()
returns an object that represents the connection between our program and transactions.txt.
The directory (or folder) where your Python script is running is called the current directory. When you open a file, Python looks for it in the current directory.
If a file you want to open is in a directory other than the current directory, you will have to specify its path. The location of a file is represented by its path, the sequence of folders that the file is stored in, plus the file's name. There are two ways to specify a path:
Note: On a windows machine, folders and file names in a path are separated by backslashes "". We know the backslash has a special purpose in Python, to escape certain characters, such as a newline "\n"; therefore, you will have to escape a backslash: "\\
" in your path to a file: "files\\transactions.txt"
. Alternatively, you can specify your path as a raw string: r"files\transactions.txt"
. On a Unix-based machine (e.g. Mac, Linux distributions), the forward slash "/" is used in paths and you don't have to worry about this issue.
When we are done with a file, we should close it with close()
:
in_file.close()
Once a file is open, we want to process the data inside the file (reading) or save data to file (writing). Consider the example transactions.txt we opened earlier.
We will use the readline()
function to read in a single line in the file (in transactions.txt this is the purchase price as a string including the newline character \n):
transaction = in_file.readline()
# note the newline printed!! repr() shows non-printable characters like \n
print(transaction, repr(transaction), type(transaction))
transaction = float(transaction)
print(transaction, type(transaction))
9.98 '9.98\n' <class 'str'> 9.98 <class 'float'>
Now, let's use use the write()
function to write the transaction price we just read in to an output file called single_transaction.txt:
# creates the file if it does not exist
# overwrites the file contents if it does exist
out_file = open("single_transaction.txt", "w")
# save the value of transaction as string
out_file.write("%.2f" %(transaction))
# close file because we are done with out_file
out_file.close()
On average, how much money do I spend per credit card transaction?
Algorithm:
def read_transaction_price(in_file):
'''
'''
# readline() returns a string, including the newline character
price = in_file.readline()
# we need to convert the string returned by readline() to a numeric value
return float(price)
def compute_total_spent():
'''
'''
total_spent = 0.0
in_file = open("transactions.txt", "r")
# read in all 5 transactions in the file
for i in range(5):
total_spent += read_transaction_price(in_file)
# close the file before in_file goes out of scope
in_file.close()
return total_spent
total_spent = compute_total_spent()
avg_spent_per_transaction = total_spent / 5.0
out_file = open("avg_transaction.txt", "w")
out_file.write("On average, you spend %.2f per transaction" %(avg_spent_per_transaction))
out_file.close()
13.42 27.19 9.98 48.56 33.71 On average, you spend 26.57 per transaction
for
Loops¶Let's rewrite our transaction code to read in as many transactions as there are in the file (instead of the hard-coded 5). Using a for
loop, <sequence>
will be all of the lines in the input file, which we can get with a call to in_file.readlines()
. Our for
loop will walk through each line one at time with a loop control variable called line
.
def compute_avg_spent():
'''
'''
# accumulator variable
total_spent = 0.0
# count the transactions
num_transactions = 0
# the input file contains lines that we will iterate through as our items
for line in in_file.readlines():
print(line)
total_spent += float(line)
num_transactions += 1
# close the file before in_file goes out of scope
in_file.close()
return total_spent / num_transactions
avg_spent_per_transaction = compute_avg_spent()
print("On average, you spend %.2f per transaction" %(avg_spent_per_transaction))
13.42 27.19 9.98 48.56 33.71 On average, you spend 26.57 per transaction
while
Loops¶Let's rewrite our transaction processing code to use a while
loop. readline()
will return an empty string when the end of the file is reached. This can be used in our Boolean condition:
def compute_avg_spent():
'''
'''
# accumulator variable
total_spent = 0.0
# count the transactions
num_transactions = 0
in_file = open("transactions.txt", "r")
# read the first line in the file
spent = in_file.readline()
# test if this line is the empty string, meaning the end of file has been reached
while spent != "":
# not end of file, process this transaction
print(spent)
total_spent += float(spent)
num_transactions += 1
# progress toward Boolean condition being False here is progress through the file
spent = in_file.readline()
# close the file before in_file goes out of scope
in_file.close()
return total_spent / num_transactions
avg_spent_per_transaction = compute_avg_spent()
print("On average, you spend %.2f per transaction" %(avg_spent_per_transaction))
13.42 27.19 9.98 48.56 33.71 On average, you spend 26.57 per transaction
On a blank sheet of paper, write the following:
Individually, solve the following problems as if they were exam questions. Each student needs to turn in their own paper to get credit for MA13.
True
or False
:7 % 2 == 3 or 12 // 10 == 2.2
9 / 2 > 4 or -3 >= 0 and -5
while
loop and a for
loop?while
loop that displays numbers in the range [3, 30] inclusive that are multiples of 3.for
loop that sums 10 randomly generated numbers in the range [4, 8] inclusive.