SlideShare a Scribd company logo
1 of 41
Download to read offline
Programming Fundamentals
Lecture 6: Files and Exception Handling
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
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
Files
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
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
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
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
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
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)
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
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
“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
“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
“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
“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
Comparing the Output
Addition Test
Name: ___________
2 + 4 = ____
6 + 5 = ____
1 + 8 = ____
9 + 2 = ____
5 + 4 = ____
6 + 4 = ____
7 + 2 = ____
5 + 3 = ____
2 + 1 = ____
9 + 7 = ____
Output to Screen Output to File
19
“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
Comparing the Output
Addition Test
Name: ___________
2 + 4 = ____
6 + 5 = ____
1 + 8 = ____
9 + 2 = ____
5 + 4 = ____
6 + 4 = ____
7 + 2 = ____
5 + 3 = ____
2 + 1 = ____
9 + 7 = ____
Output to Screen Output to File
21
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
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
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
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
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']
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
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
Exception Handling
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
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
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
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
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
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
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
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
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
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
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
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
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
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

More Related Content

Similar to ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf

Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdfRashmiAngane1
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptRaja Ram Dutta
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdfsulekha24
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdfnishant874609
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptxqover
 
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleRohitKurdiya1
 
File handling & regular expressions in python programming
File handling & regular expressions in python programmingFile handling & regular expressions in python programming
File handling & regular expressions in python programmingSrinivas Narasegouda
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugevsol7206
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxAshwini Raut
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdfbotin17097
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.pptDHARUNESHBOOPATHY
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operationsTAlha MAlik
 
File Handling in C Programming for Beginners
File Handling in C Programming for BeginnersFile Handling in C Programming for Beginners
File Handling in C Programming for BeginnersVSKAMCSPSGCT
 

Similar to ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf (20)

Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
 
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
 
File handling & regular expressions in python programming
File handling & regular expressions in python programmingFile handling & regular expressions in python programming
File handling & regular expressions in python programming
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
 
Python file handling
Python file handlingPython file handling
Python file handling
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
File Handling in C Programming for Beginners
File Handling in C Programming for BeginnersFile Handling in C Programming for Beginners
File Handling in C Programming for Beginners
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 

More from lailoesakhan

ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdfProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdflailoesakhan
 
ProgFund_Lecture_2_Data_Types_and_Selection-1.pdf
ProgFund_Lecture_2_Data_Types_and_Selection-1.pdfProgFund_Lecture_2_Data_Types_and_Selection-1.pdf
ProgFund_Lecture_2_Data_Types_and_Selection-1.pdflailoesakhan
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdflailoesakhan
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdflailoesakhan
 
ProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdfProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdflailoesakhan
 
ProgFund_Lecture_5_Recap_and_Case_Study-1.pdf
ProgFund_Lecture_5_Recap_and_Case_Study-1.pdfProgFund_Lecture_5_Recap_and_Case_Study-1.pdf
ProgFund_Lecture_5_Recap_and_Case_Study-1.pdflailoesakhan
 
ProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdfProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdflailoesakhan
 
ProgFund_Lecture7_Programming_Algorithm.pdf
ProgFund_Lecture7_Programming_Algorithm.pdfProgFund_Lecture7_Programming_Algorithm.pdf
ProgFund_Lecture7_Programming_Algorithm.pdflailoesakhan
 

More from lailoesakhan (8)

ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdfProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
 
ProgFund_Lecture_2_Data_Types_and_Selection-1.pdf
ProgFund_Lecture_2_Data_Types_and_Selection-1.pdfProgFund_Lecture_2_Data_Types_and_Selection-1.pdf
ProgFund_Lecture_2_Data_Types_and_Selection-1.pdf
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
ProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdfProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdf
 
ProgFund_Lecture_5_Recap_and_Case_Study-1.pdf
ProgFund_Lecture_5_Recap_and_Case_Study-1.pdfProgFund_Lecture_5_Recap_and_Case_Study-1.pdf
ProgFund_Lecture_5_Recap_and_Case_Study-1.pdf
 
ProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdfProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdf
 
ProgFund_Lecture7_Programming_Algorithm.pdf
ProgFund_Lecture7_Programming_Algorithm.pdfProgFund_Lecture7_Programming_Algorithm.pdf
ProgFund_Lecture7_Programming_Algorithm.pdf
 

Recently uploaded

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 

Recently uploaded (20)

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 

ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf

  • 1. Programming Fundamentals Lecture 6: Files and Exception Handling
  • 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
  • 17. Comparing the Output Addition Test Name: ___________ 2 + 4 = ____ 6 + 5 = ____ 1 + 8 = ____ 9 + 2 = ____ 5 + 4 = ____ 6 + 4 = ____ 7 + 2 = ____ 5 + 3 = ____ 2 + 1 = ____ 9 + 7 = ____ Output to Screen Output to File 19
  • 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
  • 19. Comparing the Output Addition Test Name: ___________ 2 + 4 = ____ 6 + 5 = ____ 1 + 8 = ____ 9 + 2 = ____ 5 + 4 = ____ 6 + 4 = ____ 7 + 2 = ____ 5 + 3 = ____ 2 + 1 = ____ 9 + 7 = ____ Output to Screen Output to File 21
  • 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