07-Csci333 Lecture FileIOExceptions

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 80

Lecture 7: File I/O & Exceptions

Cameron Johnson
with thanks to Dr. Yuehua Wang

FILE INPUT/OUTPUT
& EXCEPTIONS
Goals
 To read and write text files
 To use loops to process collections of data
 To write programs that manipulate files
 To learn more about strings
 To raise and handle exceptions
Problem Input file
32.0
54.0
 Suppose you are given a text 67.5
80.25
file that contains a sequence 115.0
of floating-point values,
stored one value per line
 You need to read the values and
Output file
write them to a new output file,
32.00
aligned in a column and 54.00
followed by their total and 67.50
80.25
average value 115.00
 If the input file has the contents --------
Total: 348.75
 The output file will contain Average: 69.75
File Input and Output
Introduction

 For program to retain data between the times


it is run, you must save the data
 Data is saved to a file, typically on computer disk
 Saved data can be retrieved and used at a later time
 “Writing data to”: saving data on a file
 Output file: a file that data is written to
 “Reading data from”: process of retrieving data
from a file
 Input file: a file from which data is read
Types of Files and File Access Methods

 In general, two types of files


 Text file: contains data that has been encoded as
text
 Binary file: contains data that has not been
converted to text
 Two ways to access data stored in file
 Sequential access: file read sequentially from
beginning to end, can’t skip ahead
 Direct access: can jump directly to any piece of
data in the file
Filenames and Operations
 Filename extensions: short sequences of
characters that appear at the end of a
filename preceded by a period
 Extension indicates type of data stored in the file
 Two kinds of operations with files
Basic steps
 Three steps when a program uses a file
 Open the file
 Process the file
 Close the file
 File object: object associated with a specific
file
 Provides a way for a program to work with the file:
file object referenced by a variable
Opening a File
 open function: used to open a file
 Creates a file object and associates it with a file on
the disk
 General format:

file_object = open(filename, mode)

 Mode: string specifying how the file will be


opened
 Example: reading only ('r'), writing ('w'), and
appending ('a')

infile == open("input.txt",
infile open("input.txt", "r")
"r")
outfile == open("output.txt",
outfile open("output.txt", "w")
"w")
Accessing a File
 To access a file, you must first open it
 Suppose you want to read data from a file named
input.txt, located in the same directory as the program
 To open a file for reading, you must provide the name
of the file as the first argument to the open function
and the string "r" as the second argument:
infile == open("input.txt",
infile open("input.txt", "r")
"r")

 When opening a file for reading,


 the file must exist (and otherwise be accessible) or an
exception occurs
Accessing a File

 To open a file for writing, you provide the name of


the file as the first argument to the open function
and the string "w" as the second argument:
outfile == open("output.txt",
outfile open("output.txt", "w")
"w")

 If the output file already exists, it is emptied


before the new data is written into it
 If the file does not exist, an empty file is created
Writing Data to a File
 Method: a function that belongs to an object
 Performs operations using that object
 File object’s write method used to write
data to the file
 Sytax:
file_variable.write(string)

 File should be closed using file object close


method
 Sytax:
file_variable.close()
Writing a String to a File
 For example, we can write the string "Hello,
World!" to our output file using the
statement:
outfile.write("Hello, World!\n")
outfile.write("Hello, World!\n")

 Unlike print() when writing text to an output file,


you must explicitly write the newline character to
start a new line
 You can also write formatted strings to a file
with the write method:
outfile.write("Number of
outfile.write("Number of entries:
entries: %d\nTotal:
%d\nTotal: %8.2f\n"
%8.2f\n" %%
(count, total))
(count, total))
Alternate String Formatting
 % function (printf style)
 What we’ve seen before
 print(“%s is %0.2f”, %(“pi”, math.pi)
 Returns: “pi is 3.14”
 Can specify variables by name to make it order agnostic: print(%
(specNum)s is %(specVal)0.2f, %(“specVal”:math.pi, “specNum”:“pi”))
 String.format()
 Introduced in Python 3
 Replaces % prefix with curly braces {}
 newString = “{} is {}”.format(“pi”,math.pi)
 This will return as much of math.pi as its default values support
 newString = “{specVal} is
{specNum:02.f}”.format(specVal=“pi”,specNum=math.pi)
Alternate String Formatting

 f-strings (available in Python 3.6 and later)


 Prefix your string with an f, and the contents of
the curly braces are evaluated as Python code.
 specVal = “pi”, specNum=math.pi
 f”{specVal} is {specNum}”
 returns: “pi is 3.141592”
 f”Five plus three is {5+3}”
 returns: “Five plus three is 8”
Closing Files: Important

 When you are done processing a file, be sure


to close the file using the close() method:

infile.close()
outfile.close()
 If your program exits without closing a file that
was opened for writing, some of the output may
not be written to the disk file
Syntax: Opening and Closing
Files
Example of Writing data

# This program writes three lines of data to a file.


def main():
# Open a file named philosophers.txt.
outfile = open('philosophers.txt', 'w')

# Write the names of three philosphers


# to the file.
outfile.write('John Lee\n')
outfile.write('David Smith\n')
outfile.write('Anna Wood\n')

# Close the file.


outfile.close()

# Call the main function.


main()
Reading Data From a File

 read method: file object method that reads


entire file contents into memory
 Only works if file has been opened for reading (‘r’)
 Contents returned as a string
 readline method: file object method that
reads a line from the file
 Line returned as a string, including '\n'
Example of Reading data at one time

# This program reads and displays the contents


# of the philosophers.txt file.
def main():
# Open a file named philosophers.txt.
infile = open('philosophers.txt', 'r')

# Read the file's contents.


file_contents = infile.read()

# Close the file.


infile.close()

# Print the data that was read into memory.


print(file_contents)

# Call the main function.


main()

file_contents John Lee \n David Smith \n Anna Wood \n


file_contents variable references the string that was read from the file
Example of reading data by
lines
# This program reads the contents of the
# philosophers.txt file one line at a time.
def main():
# Open a file named philosophers.txt.
infile = open('philosophers.txt', 'r')

# Read three lines from the file


line1 = infile.readline()
line2 = infile.readline()
line3 = infile.readline()

# Close the file.


infile.close()

# Print the data that was read into memory.


print(line1)
print(line2)
print(line3) John Lee \n David Smith \n Anna Wood \n

# Call the main function.


main()
Read Position
File Operations
Using Loops to Process Files

 Files typically used to hold large amounts of


data
 Loop typically involved in reading from and
writing to a file
 Often the number of items stored in file is
unknown
 The readline method uses an empty string as a
sentinel when end of file is reached
 Can write a while loop with the condition

while line != “”
Examples of reading and writng numeric data
# Prompt the user for the name of the input and output files.
inputFileName = input("Input file name: ")
outputFileName = input("Output file name: ")
# Open the input and output files.
infile = open(inputFileName, "r")
outfile = open(outputFileName, "w")
# Read the input and write the output.
total = 0.0
count = 0
line = infile.readline()

while line != "" :


value = float(line)
outfile.write("%15.2f\n" % value)
total = total + value
count = count + 1
line = infile.readline()

# Output the total and average.


outfile.write("%15s\n" % "--------")
outfile.write("Total: %8.2f\n" % total)
avg = total / count
outfile.write("Average: %6.2f\n" % avg)
# Close the files.
infile.close()
outfile.close()
Writing and Reading Numeric Data

 Numbers must be converted to strings before


they are written to a file
 str function: converts value to string

sales =1000
outfile.write(str(sales) + '\n')
 Number are read from a text file as strings
 Must be converted to numeric type in order to
perform mathematical operations
 Use int and float functions to convert string to
numeric value
Common Error
 Backslashes in File Names
 When using a String literal for a file name with
path information, you need to supply each
backslash twice:
infile == open("c:\\homework\\input.txt",
infile open("c:\\homework\\input.txt", "r")
"r")
 A single backslash inside a quoted string is the
escape character, which means the next character
is interpreted differently (for example, ‘\n’ for a
newline character)

When a user supplies a filename into a program, the user should


NOT type the backslash twice
Using Python’s for Loop to Read
Lines
 Python allows the programmer to write a
for loop that automatically reads lines in a
file and stops when end of file is reached
 The general format of the for loop
for variable in file_object:
statement
statement statements
etc.
 variable is the name of a variable, and file_object is a
variable that references a file object.
 The loop iterates once over each line in the file
Example of using for loop
# This program uses the for loop to read
# all of the values in the sales.txt file.
 
def main():
# Open the sales.txt file for reading.
sales_file = open('sales.txt', 'r')
 
# Read all the lines from the file.
for line in sales_file:
# Convert line to a float.
amount = float(line)
# Format and display the amount.
print("%.2f" % (amount)))

# Close the file.


sales_file.close()
 
# Call the main function.
main()
More about Strings
Basic String Operations

 Many types of programs perform operations


on strings
 In Python, many methods/functions for
examining and manipulating strings
 Strings are sequences, so many of the
methods/functions that work with sequences
work with strings
Accessing the Individual Characters in a String

 To access an individual character in a string:


 Use a for loop
 General Format:

 Useful
for when need
character to iterate over the whole string, such
in string:
as to count the occurrences of a specific character
 Use indexing
 Each character has an index specifying its position in
the string, starting at 0
 General Format:
character = my_string[i]
Accessing the Individual Characters in a String (cont’d.)
Another Example
 A schematic diagram
of the indices of the
string ‘foobar’ would
look like this:

 The individual
characters can be
accessed by index as
shown to the right
String Review
 len(string) function
 can be used to obtain the length of a string
 Useful to prevent loops from iterating beyond the end of a string

 length
IndexError exception will occur
= len("World!") if:
# length is 6
 You try to use an index that is out of range for the string
 Likely to happen when loop iterates beyond the end of the string
 String indices
 can also be specified with Some examples of negative indexing:

negative numbers, in which


case indexing occurs from
the end of the string
backward: 
 -1 refers to the last
character, 
 -2 the second-to-last
character, and so on
 For any non-empty string s, s[len(s)-1] and s[-
1] both return the last character. 
 Attempting to index with negative numbers
beyond the start of the string results in an
error:
String Concatenation
(Review)
 Concatenation: appending one string to the
end of another string
 Use the ‘+’ operator to produce a string that is a
combination of its operands
 The augmented assignment operator ‘+=‘ can
also be used to concatenate strings
 The operand on the left side of the ‘+=‘ operator
must be an existing variable; otherwise, an
exception is raised
Strings Are Immutable

 Strings are immutable


 Once they are created, they cannot be changed
 Concatenation doesn’t actually change the existing
string, but rather creates a new string and assigns
the new string to the previously used variable
 Cannot use an expression of the form

string[index] = new_character

 Statement of this type will raise an exception


Object Assignment
String Slicing
 Slice: span of items taken from a
sequence, known as substring
 Slicing format:
string[start:end]
 Expression will return a string containing a
copy of the characters from start up to,
but not including, end
 If start not specified, 0 is used for start
index
 If end not specified, len(string) is
used for end index
String Slicing
 For any string s and any integer n (0 ≤ n ≤ len(s)), 
 s[:n] + s[n:] will be equal to s:
String Slicing
 Slicing expressions
 can include a step value and negative indexes relative to end of string
 the slice 0:6:2 
 starts with the first character and ends with the last character (the whole string), and every
second character is skipped. 
 the slice 1:6:2 
 specifies a slice starting with the second character (index 1) and ending with the last
character and again the stride value 2 causes every other character to be skipped
Searching Strings
 use the in operator to determine whether one string is contained in
another string
 General format: string1 in string2
 string1 and string2 can be string literals or variables referencing
strings
 The in operator returns True if the first operand is contained within the second,
and False otherwise
Searching Strings
 Similarly, you can use the not in operator
 to determine whether one string is not contained
in another string
The Repetition Operator

 Repetition operator: makes multiple copies


of a string and joins them together
 The * symbol is a repetition operator when
applied to a string and an integer
 String is left operand; number is right
 General format:
string_to_copy * n
 Variable references a new string which contains
multiple copies of the original string
String Methods

 Strings in Python have many types of


methods, divided into different types of
operations
 General format:
 Some
mystring.method(arguments)
methods test a string for specific
characteristics
 Generally Boolean methods return True if a
condition exists, and False otherwise
String Methods (cont’d.)
String Methods (cont’d.)

 Some methods return a copy of the string, to


which modifications have been made
 Simulate strings as mutable objects
 String comparisons are case-sensitive
 Uppercase characters are distinguished from
lowercase characters
 lower and upper methods can be used for
making case-insensitive string comparisons
String Methods (cont’d.)
String Methods (cont’d.)
 Programs commonly need to search for substrings
 Several methods to accomplish this:
 endswith(substring): checks if the string ends with substring
 Returns True or False
 startswith(substring): checks if the string starts with
substring
 Returns True or False
String Methods (cont’d.)

 Several methods to accomplish this (cont’d):


 find(substring): searches for substring
within the string
 Returns lowest index of the substring, or if the
substring is not contained in the string, returns -1
 replace(substring, new_string):
 Returns a copy of the string where every occurrence
of substring is replaced with new_string
String Methods (cont’d.)
Splitting a String

 split method: returns a list containing the


words in the string
 By default, uses space as separator
 Can specify a different separator by passing it as
an argument to the split method
Additional String Splitting Methods
Additional String Splitting
Examples
Concatenating a Newline to and Stripping it From a String

 In most cases, data items written to a file are


values referenced by variables
 Usually necessary to concatenate a '\n' to data
before writing it
 Carried out using the + operator in the argument of
the write method
 In many cases need to remove '\n' from
string after it is read from a file
 rstrip method:
 string method that strips specific characters from
end of the string
>>> 'A line of text.\n'.rstrip('\n')
'A line of text.'
>>> 'A line of text.\n'.rstrip()
'A line of text.'
String Methods (cont’d.)
 lstrip method:
 string method that strips specific characters from the left
end of the string

 strip method
 is essentially equivalent to invoking s.lstrip() and
s.rstrip() in succession. Without specifying argument, it
removes leading and trailing whitespace:
More examples
Challenge

 You will learn how to process text with


complex contents, and you will learn how to
cope with challenges that often occur with
real data
Mary
Mary had a little had
lamb a
input output little
lamb
 Problem analysis:

 it will be split into 5 substrings that are stored in a


list in the same order in which they occur in the
string:
wordlist
wordlist == line.split()
line.split()
Solution
def main():
# Open the file for reading.
inputFile = open("lyrics.txt", "r")
for line in inputFile :
# remove the whitespaces from the end
line = line.rsplit()
# split the string
wordList = line.split()
for word in wordList :
word = word.rstrip(".,?!")
print(word)
# Close the file.
inputFile.close()
 
# Call the main function.
main()
Exception Handling
Exceptions
 Exception: error that occurs while a program is
running
 Usually causes program to abruptly halt

 Traceback: error message that gives information


regarding line numbers that caused the exception
 Indicates the type of exception and brief description of
the error that caused exception to be raised
 Many exceptions can be prevented by careful
coding
 Example: input validation
 Usually involve a simple decision construct
 Some exceptions cannot be avoided by
careful coding
 Examples
 Trying to convert non-numeric string to an integer
 Trying to open for reading a file that doesn’t exist
Raising an Exception
 use raise to throw/force an exception if a
condition occurs
 The statement can be complemented with a
custom exception.
 General format: raise exceptionObject

 For example

 When you run this code, the output will be the


following:
 Another example
Exception Classes (a subset)
Try-Except
 Exception handler: code that responds when exceptions are raised
and prevents program from crashing
 In Python, written as try/except statement
 General format:

try: try:
Statements Statements
except exceptionType: except exceptionType:
Statements Statements
 Try suite: statements that can potentially raise an
except exceptionType as
exception varName:
Statments
 Handler: statements contained in except block
Try-Except: An Example
try ::
try
filename == input("Enter
filename input("Enter filename:
filename: ")
")
open() can raise an
infile == open(filename,
infile open(filename, "r")
"r")
IOError exception
line = infile.readline()
line = infile.readline()
value == int(line)
value int(line) int() can raise a
.. .. .. ValueError exception
except IOError
except IOError :: Execution transfers here if
print("Error: file
print("Error: file not
not found.")
found.") file cannot be opened
except ValueError
except ValueError as
as exception
exception ::
Execution transfers here if
print("Error:", str(exception))
print("Error:", str(exception))
the string cannot be
converted to an int

If either of these exceptions is raised, the rest of the


instructions in the try block are skipped
 If statement in try suite raises exception:
 Exception specified in except clause:
 Handler immediately following except clause executes
 Continue program after try/except statement
 Other exceptions:
 Program halts with traceback error message

 If no exception is raised,
 handlers are skipped
Handling Multiple Exceptions
 Often code in try suite can throw more than one type of exception
 Need to write except clause for each type of exception that needs to
be handled
 An except clause that does not list a specific exception will handle
any exception that is raised in the try suite
 Should always be last in a series of except clauses
The else Clause
 try/except statement may include an optional else clause,
which appears after all the except clauses
 Aligned with try and except clauses
 Syntax similar to else clause in decision structure

try:
Statements
except
exceptionType:
 else
Statements
suite: block of statements executed after statements in try suite,
else:
only if no exceptions were raised
 Statments
If exception was raised, the else suite is skipped
The finally Clause
 try/except statement may include an optional finally clause, which
appears after all the except clauses
 Aligned with try and except clauses
 General format:
try:
Statements
except
exceptionType:
Statements
else:
Statments
finally:
Statments

 Finally suite: block of statements after the finally clause


 Execute whether an exception occurs or not
 Purpose is to perform cleanup before exiting
Example

 try/exception/else/finally

outfile = open("demofile.txt","w")
try:
outfile.write("Hello World")
except:
print("Something went wrong when writing to the file")
else:
print("Well done")
finally:
outfile.close()
Homework 7
 Create a python script that will load homework7WordSearchKey.txt and use it to
create a word search puzzle, then save that as homework7WordSearchPuzzle.txt
 The example of this part was done in class; you may use as much of that as you like as long
as you ensure it works on your computer.
 Create a second python script that will load your
homework7WordSearchPuzzle.txt as well as homework7WordSearchList.txt, and
then search the puzzle you’ve created and recreate the block from
homework7WordSearchKey.txt
 The horizontal words should be almost trivial
 The vertical words will require some careful thought on how to manipulate the strings,
and/or how to load the data.
 There is a single diagonal word that will likely be the most challenging
 Your code should be able to find words that are in the word list without knowing in advance
which direction they are facing (though no words will be backwards)
 This homework is due in two weeks, on 30 March 2021 at 6:30 am.
 There will be a homework next week, however, so do not put this one off; the extra time is
due to the complexity of the problem.

You might also like