- Python Try Except
- Exception Handling
- Example
- Example
- Many Exceptions
- Example
- Else
- Example
- Finally
- Example
- Example
- Raise an exception
- Example
- Example
- Files and Exceptions in Python
- Prerequisites
- Reading from a file
- Working with the contents of a file
- Writing to a file
- Writing to an empty file
- Appending to a file
- Exceptions
- Handling the ZeroDivisionError exception
- Handling the FileNotFoundError exception
- Conclusion
- Further reading
Python Try Except
The try block lets you test a block of code for errors.
The except block lets you handle the error.
The else block lets you execute code when there is no error.
The finally block lets you execute code, regardless of the result of the try- and except blocks.
Exception Handling
When an error occurs, or exception as we call it, Python will normally stop and generate an error message.
These exceptions can be handled using the try statement:
Example
The try block will generate an exception, because x is not defined:
Since the try block raises an error, the except block will be executed.
Without the try block, the program will crash and raise an error:
Example
This statement will raise an error, because x is not defined:
Many Exceptions
You can define as many exception blocks as you want, e.g. if you want to execute a special block of code for a special kind of error:
Example
Print one message if the try block raises a NameError and another for other errors:
try:
print(x)
except NameError:
print(«Variable x is not defined»)
except:
print(«Something else went wrong»)
Else
You can use the else keyword to define a block of code to be executed if no errors were raised:
Example
In this example, the try block does not generate any error:
Finally
The finally block, if specified, will be executed regardless if the try block raises an error or not.
Example
This can be useful to close objects and clean up resources:
Example
Try to open and write to a file that is not writable:
try:
f = open(«demofile.txt»)
try:
f.write(«Lorum Ipsum»)
except:
print(«Something went wrong when writing to the file»)
finally:
f.close()
except:
print(«Something went wrong when opening the file»)
The program can continue, without leaving the file object open.
Raise an exception
As a Python developer you can choose to throw an exception if a condition occurs.
To throw (or raise) an exception, use the raise keyword.
Example
Raise an error and stop the program if x is lower than 0:
if x < 0:
raise Exception(«Sorry, no numbers below zero»)
The raise keyword is used to raise an exception.
You can define what kind of error to raise, and the text to print to the user.
Example
Raise a TypeError if x is not an integer:
if not type(x) is int:
raise TypeError(«Only integers are allowed»)
Files and Exceptions in Python
Files are identified locations on a disk where associated data is stored. Working with files will make your programs fast when analyzing masses of data. Exceptions are special objects that any programming language uses to manage errors that occur when a program is running.
In this tutorial, we’ll learn about file operations in Python. These operations include reading, writing, and appending to a file. We will also learn about handling the ZeroDivisionError and FileNotFoundError exception.
Prerequisites
Have some basic knowledge of the Python coding language.
Reading from a file
When working with data from a text file, you must first read it into memory. To read a file, you must have a file that exists.
Let’s create a text file containing a list of years from 2020 to 2022 using an editor and save it in the same folder that stores our Python files as years.txt .
The years.txt file should have the following text:
Below is a program that opens the above file, reads it and prints the data in the file:
with open('years.txt') as file_object: contents = file_object.read() print(contents)
The name of the file to be opened is passed to the open() function as its only argument. Python looks for the years.txt file in the folder, where our Python file is stored.
The open() function returns an object representing the file (years.txt) which is then stored in variable file_object.
The keyword with closes the file when access to it is no longer need.
The read() method is used to read the whole data in the file and store it in contents.
We get the following results after we run the above code:
Working with the contents of a file
Now, you have learned how to read a file into memory, let’s try doing something with that data.
Let’s create a single line holding all the digits in the years.txt file without white spaces in it as shown below:
with open('years.txt') as file_object: lines = file_object.readlines() yrs_string = '' # create a variable yrs_string to hold the digits of years for line in lines: # create a loop that adds each line of digits to yrs_string yrs_string += line.rstrip() #.rstrip() removes the newline character from each line print(yrs_string) # print this string print(len(yrs_string)) # print how long the string is # OUTPUT # 209020702089 # 12
NOTE: Python treats all text in a text file as a string. If you read a number from a file and you want to carry out arithmetic operations, convert it to float using the float() function or integer using the int() function.
Writing to a file
One of the simplest ways to save data, is to write to a file. The output will still be even after we close the terminal having our program’s outputs.
When writing text to a file, we use the open() function with two arguments — the first argument is the filename, while the second argument is the mode in which you want to open the file.
There are four modes in which you can open a file:
Writing to an empty file
If the file you are writing does not exist at all, the open() function auto-generates the file.
with open('student.txt', 'w') as file_object: file_object.write("My name is Felix.")
The above code doesn’t print the output on the terminal, but when you open the student.txt file, you will see one line:
NOTE: When opening a file in ‘w’ mode and the file exists, Python will delete the file before returning the file object.
Appending to a file
To add data into a file, open it in append mode. Any data you write will be placed at the end of the file.
Let’s add some lines in the student.txt file:
with open('student.txt', 'a') as file_object: #’a’ argument to open the file for appending file_object.write("I am 6 years old\n") file_object.write("I love playing games\n")
The new student.txt file looks like this:
My name is Felix. I am 6 years old I love playing games
Exceptions
Exceptions are unique objects that Python uses to control errors that occur when a program is running. Exceptions errors arise when correct syntactically Python programs produce an error.
Python creates an exception object whenever these mistakes occur. When we write code that deals with the exception, our programs will continue running, even if an error is thrown. If we don’t, our programs will stop executing and show a trace-back, which is very hard for a user to understand.
Python uses the try-except-finally block to control exceptions. A try-except block informs Python how to act when an exception emerges. Our programs will continue to execute even if things go wrong.
Handling the ZeroDivisionError exception
Let’s run a program that divides a number by zero. We know it is impractical, but let’s see what Python does:
When we run the above code Python gives the following trace-back:
Traceback (most recent call last): File “C:\Users\ADMIN\PycharmProject\pythonProject\main.py”, line 1, in print(6/0) ZeroDivisionError: division by zero
Since Python cannot divide a number by zero, it reports an error in the trace-back as ZeroDivisionError, which is an exception object. This kind of object responds to a scenario where Python can’t do what we asked it to.
If you think an error might occur, use the try-except block to control the exception that may be raised.
To handle the ZeroDivisionError exception, use a try-except block like this:
try: print(6/0) except ZeroDivisionError: print("You can’t divide by zero!") # You can’t divide by zero!
Handling the FileNotFoundError exception
Errors will always arise when working with files that are missing. Python may fail to retrieve a file, if you have written the wrong spelling of the filename, or the file does not exist.
We handle this situation by applying the try-except block.
For example, the program below tries to read a file that doesn’t exist on my computer:
filename 'John.txt' with open(filename) as f_obj: contents = f_obj.read()
Since Python can not read a file that does not exist, it raises an exception:
Traceback (most recent call last): File “C:\Users\ADMIN\PycharmProject\pythonProject\main.py”, line 2, in with open(filename) as f_obj: FileNotFoundError: [Errno 2] No such file or directory: ‘john.txt’
Since Python can not find the file, we are opening it creates an exception that is the FileNotFoundError exception.
In this example, the open() function creates the error. To solve this error, use the try block just before the line, which involves the open() function:
filename = 'John.txt' try: with open(filename) as f_obj: contents = f_obj.read() except FileNotFoundError: msg = "Sorry, the file "+ filename + "does not exist." print(msg) # Sorry, the file John.txt does not exist.
Conclusion
In this article, we have learned how to:
- Read from a file
- Work with a file contents
- Write to a file
- Handle the ZeroDivisionError exception
- Handle the FileNotFoundError exception
Further reading
For more information about files and exceptions in Python, see the links below:
Peer Review Contributions by: Srishilesh P S