2. This Lecture
⢠Files
â Opening, using and closing files
â File opening modes
â Writing to files
â Reading entire files, reading lines and bytes
⢠Exception handling
â Exception handlers, termination and continuation
â Exception names
â Try and catch/except
â Else and finally
2
3. Textbook
⢠Starting Out with Python, 4th Edition (Global)
â Tony Gaddis
⢠Reading the chapter(s) is required
â Read the indicated chapter(s) before class
⢠This week covers the following textbook chapter(s):
â Chapter 6 â Files and Exceptions
⢠Entire chapter
3
5. Files
⢠So far, our programs have been entirely âself-containedâ
â Nothing is stored or kept between each run of the program
â Input we provide to the program is stored in RAM, output is shown on the screenâŚ
Nothing lasts beyond end of program
⢠To retain data, receive input or store output, programs often use files on the
computerâs hard drive:
â Settings and configuration files of programs
â âOffice Suiteâ files such as word processed documents, spreadsheets and slideshows
â Media files created and used by image, video and music apps
â Cache and cookie files created and used by web browsers
â Assets, save files and replays of gamesâŚ
5
6. Files
⢠Files can be used for both input and output
â Reading data from a file allows you to use it as input
â Writing data to a file allows you to store/save output in it
⢠Files come in two main types:
â Text files simply contain text, encoded in ASCII or Unicode, and can be opened by
programs like Notepad
⢠Text can be formatted or structured in specific ways to make it more useful, e.g. CSV, JSON
⢠The extension is not necessarily â.txtâ â e.g. Python programs (â.pyâ) are just text files
â Binary files are not text-based and are designed to only be opened by a program that
can use the data in that format
⢠Includes everything that isnât a text file (images, exes, audio files, video filesâŚ)
6
7. Files
⢠Working with files in a program involves the same general process, regardless of
the programming language you are using:
1. Open the file in the program:
⢠If reading from a file, it must obviously exist
⢠If writing to a file, it must be created if it doesnât exist. If it does, you can either overwrite its
contents or modify/append to them
â Opening a file in a program leaves you with a variable representing it (a âfile objectâ),
which you interact with in the following steps
2. Use the file
⢠Reading data from the file, writing data to the file, or both
3. Close the file
⢠Once you have finished using the file, closing it severs its connection with the program and
releases the resources. It also ensures that anything you wrote to the file is actually saved
7
8. Opening a File
⢠Opening a file requires you to specify the filename
â If the file does not exist, it will be created
â If the file is not in the same folder as the program, you must specify the path to the file
⢠You also typically need to specify the mode to open it in
â This controls what you can do with the file once it is open
â These are the most widely-implemented modes, however modes and their
abbreviations can differ slightly between languages:
⢠Read (ârâ) allows reading only (file must exist)
⢠Write (âwâ) allows writing only (creates or overwrites the file, starting out empty)
⢠Append (âaâ) allows writing only (writes to the end of the file, instead of overwriting it)
⢠Read/Write (ârwâ / âr+â / âw+ââŚ) allows reading and writing (slight differences in each mode)
â Some languages require different modes depending on whether it is a text or binary file
8
9. Opening a File
⢠In Python, the built-in function âopen()â opens a file and returns a file object
â In some languages, a module is needed to interact with files
â These examples open a file named âfile.txtâ, in âreadâ mode, into a variable named âfâ:
⢠If the file is in another folder, you must specify the path:
â The ârâ at the start of the string makes Python treat it as a ârawâ string - treating any
backslashes in the string as literal characters rather than escape characters
⢠This is useful when the string contains backslashes, which are likely in path names
9
# no module necessary
f = open('file.txt', 'r')
Python
import java.io.FileReader;
FileReader f = new FileReader("file.txt");
Java
// no module necessary
$f = fopen('file.txt', 'r');
PHP
#include <stdio.h>
FILE *f = fopen("file.txt", "r");
C / C++
# no module necessary
f = File.open('file.txt', 'r')
Ruby
f = open(r'D:otherstufffile.txt', 'r') Python
10. Writing to a File
⢠Once the file is open in a mode that allows writing, you just need to write data to
it using a âwriteâ function
â Similar to using âprint()â, but instead of displaying on the screen, it writes to a file
â Most of the time, you will be starting out from a new/blank file, rather than appending
⢠Writing to a file uses the variable/object created when the file was opened
â Either by using the âwriteâ method (function) of the file object:
â Or by passing the variable to a âwriteâ function as a parameter:
12
f = open('file.txt', 'w')
f.write('This text will be written to the file!')
f.close()
Python
$f = fopen('file.txt', 'w');
fwrite($f, 'This text will be written to the file!');
fclose($f);
PHP (âfwrite()â is a standalone
function that you pass the
file variable and text to)
(âwrite()â is a function that
you call on the file object
and pass the text to)
11. Writing to a File
⢠Note that while âprint()â automatically adds a line break at the end of your line,
â.write()â does not:
⢠Use ânâ to insert line breaks where you need them:
f = open('joke.txt', 'w')
f.write('What do you call a group of 8 hobbits?')
f.write('A hobbyte.')
f.close()
Python
f = open('joke.txt', 'w')
f.write('What do you call a group of 8 hobbits?n')
f.write('A hobbyte.')
f.close()
Python
13
12. Closing a File
⢠Once you have finished interacting with the file in your program, you must close it
â Not doing this can result in losing the data you wrote, since the data is often written to
a buffer in memory and only saved to the file on the hard drive when the file is closed
â Not closing a file can also keep it âlockedâ; Unable to be used by other programs
⢠This involves using the âcloseâ method on the file object or calling the function to
close a file, depending on language:
⢠Ideally, minimise the time that a file is open for, e.g.:
â Open it in âreadâ mode, read the entire text into a variable, close it
â Open/Create it in âwriteâ mode, write everything at once, close it
f = open('file.txt', 'r')
f.close()
Python $f = fopen('file.txt', 'r');
fclose($f);
PHP
14
13. âTest Generatorâ Case Study â Review of Version 1
⢠Last week we went over a case study, designing and writing a program to
generate randomised maths tests
Prompt user for + or - and store in 'op' variable.
If op is '+'
Print 'Addition Test'
Else
Print 'Subtraction Test'
Set op to '-'
Print 'Name: __________'
Loop 10 times
Generate random number between 0 and 9 and store it as 'num_one'
Generate random number between 0 and 9 and store it as 'num_two'
If op is '-' and num_one < num_two
Swap num_one and num_two
Print 'num_one op num_two = ____'
Pseudocode
15
14. âTest Generatorâ Case Study â Review of Version 1
⢠Last week we went over a case study, designing and writing a program to
generate randomised maths tests
import random
op = input('Addition or subtraction test? [enter "+" or "-"]: ')
if op == '+':
print('Addition Testn')
else:
print('Subtraction Testn')
op = '-'
print('Name: __________nn')
for i in range(10):
num_one = random.randint(0, 9)
num_two = random.randint(0, 9)
if op == '-' and num_one < num_two:
num_one, num_two = num_two, num_one
print(num_one, op, num_two, '= ____n')
Python
Addition Test
Name: ___________
2 + 4 = ____
6 + 5 = ____
1 + 8 = ____
9 + 2 = ____
5 + 4 = ____
6 + 4 = ____
7 + 2 = ____
5 + 3 = ____
2 + 1 = ____
9 + 7 = ____
Note the extra line breaks (ânâ) to add extra blank lines in the output
(remember, print() ends with a line break automatically)
16
15. âTest Generatorâ Case Study â Writing to a File
⢠Now letâs change the program so that it writes its output to a file named
âoutput.txtâ instead of showing it on the screen
Prompt user for + or - and store in 'op' variable.
Create and open 'output.txt' file in write mode
If op is '+'
Write 'Addition Test' to output file
Else
Write 'Subtraction Test ' to output file
Set op to '-'
Write 'Name: __________' to output file
Loop 10 times
Generate random number between 0 and 9 and store it as 'num_one'
Generate random number between 0 and 9 and store it as 'num_two'
If op is '-' and num_one < num_two
Swap num_one and num_two
Write 'num_one op num_two = ____' to output file
Close the output file
Print message informing user of the file
Pseudocode
17
16. âTest Generatorâ Case Study â Writing to a File
⢠Now the code creates/opens âoutput.txtâ, and instead of printing the output, writes
it to the file before closing it
import random
op = input('Addition or subtraction test? [enter "+" or "-"]: ')
output = open('output.txt', 'w')
if op == '+':
output.write('Addition Testn')
else:
output.write('Subtraction Testn')
op = '-'
output.write('Name: __________nn')
for i in range(10):
num_one = random.randint(0, 9)
num_two = random.randint(0, 9)
if op == '-' and num_one < num_two:
num_one, num_two = num_two, num_one
output.write(str(num_one) + ' ' + op + ' ' + str(num_two) + ' = ____n')
output.close()
print('Test saved in output.txt file.')
Python
18
18. âTest Generatorâ Case Study â Writing to a File
⢠Remember, since â.write()â doesnât add a line break to the end like
âprint()â does, we need to add them⌠An extra ânâ at the end of each line!:
import random
op = input('Addition or subtraction test? [enter "+" or "-"]: ')
output = open('output.txt', 'w')
if op == '+':
output.write('Addition Testnn')
else:
output.write('Subtraction Testnn')
op = '-'
output.write('Name: __________nnn')
for i in range(10):
num_one = random.randint(0, 9)
num_two = random.randint(0, 9)
if op == '-' and num_one < num_two:
num_one, num_two = num_two, num_one
output.write(str(num_one) + ' ' + op + ' ' + str(num_two) + ' = ____nn')
output.close()
print('Test saved in output.txt file.')
Python
20
20. Reading from a File
⢠Reading data from a file can be very useful
â Reading data from files previously created and written to by your program allows it to
ârememberâ things, e.g. Settings for the program, or loading previously saved data
â Programs can also read and use data from files created by other programs
⢠This relies on the fileâs data being in a specific format, so that it can be read correctly by
different programs
â We will continue to work with text files, since they have a simple format that is easy to
understand and work with
⢠Reading from a file allows your program to have a new source of input;
Something other than the user typing
Be sure to go through Reading 7.1 next week â it goes over some ways of
giving structure to text files and is likely to be relevant to Assignment 2!
22
21. Reading from a File
⢠Like writing, reading a file uses the variable/object created when the file was
opened. The same two approaches existâŚ
â Using a âreadâ method of the file object:
â Passing the file variable to a âreadâ function as a parameter:
⢠The âreadâ methods/functions will return the data they read
â Store it in a variable so that you can then use the data in your program
â Most of the time you will read an entire file and store it in a variable,
and then interact with the variable (rather than reading a file one piece at a time)
23
f = open('file.txt', 'r')
file_data = f.read(10)
f.close()
Python
$f = fopen('file.txt', 'r');
$file_data = fread($f, 10);
fclose($f);
PHP
22. Quickly Reading an Entire File
⢠The simplest way to read a file is to read the entire thing at once, giving you a
variable containing the entirety of the fileâs content
â Some languages can open, read and close a file in a single function
â Other languages have the ability to read all of a file at once, but still require you to
open and close it in separate statements
â Some languages (e.g. C/C++) require you to specify how many bytes or characters to
read. To read the entire file in such languages, canâŚ
1) Use other statements to determine the total size/length of the file, and read that many
2) Use a loop to continually read bytes/characters until you reach end of file
24
$file_string = file_get_contents('file.txt'); //returns a string
$file_array = file('file.txt'); //returns an array (one item per line of text)
PHP
f = open('file.txt', 'r')
file_data = f.read() # not giving read() a number will read the whole file
f.close()
Python
23. Reading Bytes and Reading Lines
⢠The â.read()â method in Python can be given an integer parameter to specify
how many bytes/characters to read
â Or use the âreadline()â method to read a whole line
(all data until the next line break - ânâ - or the end of the file)
⢠â.read()â and â.readline()â read data from the current âpositionâ in the file.
When you open a file in read mode, the position is set to the start of the file
â After you read data, the position moves to the end of that data
â Hence, you can use â.read()â and/or â.readline()â to work your way through a
file from beginning to end
25
f = open('file.txt', 'r')
data = f.read(10) # read 10 bytes
f.close()
Python f = open('file.txt', 'r')
data = f.readline() # read a line
f.close()
Python
24. Reading Through a File
⢠A âforâ loop will work its way through a file line by line:
⢠The â.readlines()â (note the âsâ) method puts all lines of a file into a list:
26
f = open('veg.txt', 'r')
for line in f:
print(line)
f.close()
Python
f = open('veg.txt', 'r')
file_list = f.readlines()
f.close()
Python
Leek
Potato
Carrot
file_list = ['Leekn',
'Potaton',
'Carrot']
25. Reading and Writing Numbers
⢠â.read()â and â.readline()â will always return a string, even if the data is
numeric (just like the âinput()â function)
â Use âint()â or âfloat()â to convert the data to a number if needed:
â The â.write()â method can only write strings; Use âstr()â to convert numbers to
strings, and add a ânâ if needed:
f = open('numbers.txt', 'r')
num_one = f.readline()
num_two = int(f.readline())
f.close()
Python
num_one set to '123n' # string
num_two set to 456 # int
f = open('numbers.txt', 'a')
num_three = 789
num_four = 123
f.write(str(num_three) + 'n' + str(num_four))
f.close()
Python
28
26. Files Summary
⢠Files allow you to store and use data external to a program
â i.e. Files allow for a new source of input and new destination for output
â Files can be either text or binary files
⢠To use a file, you must first open / create it
â This creates a file object/variable that is used to interact with it
â Files can be opened in various modes, read, write, appendâŚ
⢠Once a file is open, you can:
â Write data to it (from the beginning, or appending to the end)
â Read data from it (all at once, by byte, by line, line-by-lineâŚ)
⢠Once you have finished interacting with a file, close it
29
28. Exception Handling
⢠An exception is an error or condition that occurs while a program is running,
causing it to âcrashâ or otherwise end
â Youâve no doubt seen a few during the unit, usually related to input and data types,
e.g. trying to convert letters to integer:
⢠Exceptions are situations that can occur that the program cannot handle,
despite the code being syntactically valid
â Unlike syntax errors, which occur when the code is not valid and will prevent the
program from running
num = int(input('Type a number: '))
print('You typed:', num)
Python
Type a number: nope
Traceback (most recent call last):
File "H:/PythonCode/exception_example.py", line 1, in <module>
num = int(input('Type a number: '))
ValueError: invalid literal for int() with base 10: 'nope'
31
29. Exception Handling
⢠Exception handling is, unsurprisingly, the process of responding to exceptions
when they occur â i.e. handling them
â When an exception occurs, it is said to have been âraisedâ, âthrownâ or âtriggeredâ,
depending on the language
â Your response will depend on the nature of the exception:
⢠The program may be unable to continue, hence the response will be to display an error
message and end in a clean manner â known as âterminationâ
⢠The program may be able to continue, e.g. re-prompting for input or performing some other
action to work around the exception â known as âcontinuationâ
â Exception handling is essentially another control structure, like selection and iteration
(loop) statements:
⢠It allows you to control the flow of execution in a program when an exception occurs,
transferring control to an exception handler
32
30. Exception Handling
⢠So far in the unit, we have not handled the majority of exceptions that could occur
in the code we have written
â e.g. Weâve assumed that the user enters a number when prompted to enter a number
(which can then be converted to an int/float without an exception occurringâŚ)
â This has been to keep our code simple and understandable, avoiding added
complexity while we are learning the basics
⢠However we have implemented exception handling on occasion,
e.g. re-prompting for input until a number is typed
â e.g. Tasks 3 and 4 of Workshop 4 used exception handling in the âinput_int()â
and âinput_float()â functions we wrote
⢠It is important that you consider (and handle) the exceptions that could potentially
occur in the code that you write
33
31. Basic Exception Handling - Termination
⢠In this example, the exception handler ends the program:
â If a âValueErrorâ exception is raised when trying to convert the ânumâ variable to an
integer, the exception handler runs
⢠The exception handler prints an error message, then uses âsys.exit()â to end the
program (importing âsysâ to do so)
â If the exception is not raised (i.e. the conversion to integer was successful), the
exception handler does not run
⢠The code continues, printing the âYou typedâ lineâŚ
try: # prompt for a number and try to convert it to an integer
num = int(input('Type a number: '))
except ValueError: # show error message and exit
print('Invalid input. Ending program.')
import sys
sys.exit()
print('You typed:', num)
Python
Type a number: nope
Invalid input. Ending program.
34
32. Basic Exception Handling - Continuation
⢠This exception handler continues the program:
â If a âValueErrorâ exception is raised when trying to convert the ânumâ variable to an
integer, the exception handler runs
⢠The exception handler just puts an integer of 7 into ânumâ
⢠It does not end the program â the code then continues to the âYou typedâ line after the
exception handler ends
â While this example is quite simplistic, it illustrates the concept of handling an
exception without terminating the program
try: # prompt for a number and try to convert it to an integer
num = int(input('Type a number: '))
except ValueError: # just use 7
num = 7
print('You typed:', num)
Python
Type a number: nope
You typed: 7
35
33. Basic Exception Handling - Continuation
⢠This exception handler also continues the program:
â This version places the try/except code inside an endless loop
⢠If a âValueErrorâ exception is raised, the code does not reach the âbreakâ statement,
instead going immediately to the exception handler
⢠After printing the error message, the end of the loop body is reached â back to âtry:ââŚ
⢠If the exception is not raised, the code reaches the âbreakâ statement
â This ends the loop, allowing the program to reach the âYou typedâ lineâŚ
36
while True: # endless loop
try: # prompt for a number and convert it to an integer
num = int(input('Type a number: '))
break # break out of the loop
except ValueError: # show error message
print("Invalid input, try again.")
print('You typed:', num)
Python
Type a number: nope
Invalid input, try again.
Type a number: ok, fine
Invalid input, try again.
Type a number: 6
You typed: 6
34. Exception Names
⢠Different types of exceptions can occur, and each type of exception that the
language recognises is given a name
â The exceptions that different languages recognise and the names they have differ
⢠Some of the common exceptions in Python are:
â NameError, raised when you try to refer to a variable or function that doesnât exist
⢠e.g. spelling its name wrong
â TypeError, raised when you try to use the wrong data type
⢠e.g. round('this is a string', 2)
â ValueError, raised when you use the right data type, but the value is not appropriate
⢠e.g. int('not numeric')
â IndexError, raised when you try to refer to an index that doesnât exist
⢠e.g. referring to an index of 5 in a list of 3 items
37
35. Try and Catch/Except
⢠Most languages implement exception handling as follows:
â Code that might cause an exception is placed in a âtryâ block
â This is followed by a âcatchâ block, containing the code that will be run if (and only if)
the exception occurs
⢠In Python, the word âexceptâ is used instead of âcatchâ
⢠If the exception occurs, the program immediately goes to the start of the âcatchâ block
(skipping later lines in the âtryâ block)
try:
<statement(s) that can cause exceptions>
catch <exception name>:
<statements(s) to run if exception occurs>
This is known as an
âexception handlerâ
38
36. Try and Catch/Except
⢠It is optional to specify an exception name after âexceptâ
â If you just write âexcept:â, that exception handler will catch all exceptions â useful if
various things could go wrong and it is appropriate to handle them all in the same way
⢠You can specify multiple âexceptâ blocks after a âtryâ block
â You can specify a different exception name in each one, allowing you specify different
exception handlers for each different exception that can occur in the âtryâ blockâŚ
try:
<statement(s) that can cause exceptions>
except <exception name>:
<exception handler for that exception>
except:
<exception handler for any other exception>
39
37. Try and Catch/Except
⢠This example demonstrates multiple exceptions/handlers:
40
Try to:
Prompt for a filename and store in 'filename' variable
Open the file named in 'filename' in read mode
Read all lines of the file into 'line_list' list variable
Print the number of lines
Prompt for which line to view and store in 'line_num' int variable
Print line of the file, using 'line_num' - 1 as index of 'line_list'
Except if the file cannot be found:
Print appropriate error message
Except if the userâs input cannot be converted to an integer:
Print appropriate error message
Except if an invalid line number is specified:
Print appropriate error message
Except if any other exception occurs:
Print generic error message
Pseudocode
38. Try and Catch/Except
⢠This example demonstrates multiple exceptions/handlers:
try:
filename = input('File name to open: ')
f = open(filename, 'r') # FileNotFoundError will occur if the file does not exist
line_list = f.readlines()
f.close()
print('File contains', len(line_list), 'lines.')
line_num = int(input('View which line?: ')) # ValueError will occur if input is not an int
print(line_list[line_num - 1]) # IndexError will occur if the number is not a valid index number
except FileNotFoundError:
print('File not found. Check spelling.')
except ValueError:
print('Invalid input. Int required.')
except IndexError:
print('Invalid line number.')
except:
print('Something went wrong.')
Python
41
39. Try and Catch/Except
⢠Letâs look at the flow of execution through the program:
â
âĄ
â˘
âŁ
â¤
try:
filename = input('File name to open: ')
f = open(filename, 'r') # potential FileNotFoundError
line_list = f.readlines()
f.close()
print('File contains', len(line_list), 'lines.')
line_num = int(input('View which line?: ')) # potential ValueError
print(line_list[line_num - 1]) # potential IndexError
except FileNotFoundError:
print('File not found. Check spelling.')
except ValueError:
print('Invalid input. Int required.')
except IndexError:
print('Invalid line number.')
except:
print('Something went wrong.')
Python
File name to open: file.txt
File contains 4 lines.
View which line?: nope
Invalid input. Int required.
42
40. Else and Finally
⢠After all the âexceptâ blocks, you can optionally include:
â An âelseâ block, which will only run if no exceptions occurred
â A âfinallyâ block, which will always run
⢠Runs after the âtryâ block if no exceptions occur, or after the exception handler if they do
⢠This is often used for âcleanupâ (e.g. closing a file/connection), since it is sure to always run
try:
try statement 1
try statement 2
try statement 3
except <exception name>:
handler statements
else:
else statements
try:
try statement 1
try statement 2
try statement 3
except <exception name>:
handler statements
finally:
finally statements
43
41. Exception Handling Summary
⢠Exception handling allows you to catch and handle errors or conditions that
would otherwise cause a program to crash
⢠Statements that can raise an exception are placed in a âtryâ
â If an exception occurs when run the statements, the program immediately goes to the
exception handler for it
⢠Exception handlers are âcatchâ/âexceptâ blocks after a âtryâ that are triggered
when the exception they name occurs
â Catch all exception handlers are also possible
â Exception handlers may terminate the program or continue it
⢠âelseâ and âfinallyâ blocks can expand the capabilities of exception handling
44