OS Module and Exception Handling
-
os Module
-
What Is It?
This os module provides a portable way of using operating system dependent functionality.
-
How Does It Work?
The code below uses the os module to display the current working directory. This is equivalent to the console command ‘pwd’.
import os
cwd = os.getcwd()
cwd
# '/Users/rd/'
-
Paths
A relative path relates to the location of a file relative to the current working directory.
An absolute path is a path that begins from the root directory of the operating system. Paths that start with a / (from the root directory) are not relative to the current directory.
import os
os.path.abspath('README.md')
# '/Users/rd/Dev/my_awesome_project/README.md'
-
Verify a file exists.
import os
os.path.exists('README.md')
# True
-
Verify a path points to a directory.
import os
os.path.isdir('/Users/rd')
# True
-
Verify a path points to a file.
import os
os.path.isfile('/Users/rd')
# False
-
List the contents of a directory.
import os
os.listdir('/Users/rd')
# ['Applications', 'Desktop', 'Documents', 'Downloads', '.bash_profile']
-
Exceptions
Part 1
-
What Is It?
Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions.
fin = open('i_like_turtles.py', 'r')
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# FileNotFoundError: [Errno 2] No such file or directory: 'i_like_turtles.py'
-
The Try Statement
The try statement specifies exception handlers and/or cleanup code for a group of statements.
try:
fin = open('i_like_turtles.py', 'r')
except FileNotFoundError:
print('The file does not exist!')
# The file does not exist!
-
The except Clause
The except clause(s) specifies one or more exception handlers. When no exception occurs in the try clause, no exception handler is executed.
try:
print(100 / 10)
except ZeroDivisionError:
print("Division by zero is not allowed.")
# 10.0
-
The else Clause
A way to specify a block of code should be run only if no exceptions were raised at all.
try:
print(100 / 10)
except ZeroDivisionError:
print("Division by zero is not allowed.")
else:
print("No issues were encountered.")
# 10.0
# No issues were encountered.
try:
print(100 / 0)
except ZeroDivisionError:
print("Division by zero is not allowed.")
else:
print("No issues were encountered.")
# Division by zero is not allowed.
-
The finally clause
If finally is present, it specifies a ‘cleanup’ handler.
try:
print(100 / 10)
except ZeroDivisionError:
print("Division by zero is not allowed.")
finally:
print("This runs no matter what happened.")
# 10.0
# This runs no matter what happened.
try:
print(100 / 0)
except ZeroDivisionError:
print("Division by zero is not allowed.")
finally:
print("This runs no matter what happened.")
# Division by zero is not allowed.
# This runs no matter what happened.
-
Exceptions
Part 2
-
Catching Multiple Exceptions
- There can be multiple exception handlers.
- If an exception occurs, the handler searches for a a clause that matches the exception.
- If an expression-less except clause is present, it must be declared last.
try:
...
except FileNotFoundError:
...
except PermissionError:
...
except:
# No other exceptions matched
-
Catching Multiple Exceptions With A Base Class
If the logic to handle two or more exceptions is the same, a base class can be used.
try:
...
except OSError: # matches FileNotFoundError, PermissionError, and others...
...
-
Catching Multiple Exceptions With A Tuple
If the logic to handle two or more exceptions is the same, a tuple can be used.
try:
...
except (FileNotFoundError, PermissionError):
...
-
Accessing The Exception Object
import logging
try:
...
except Exception1 as e:
logging.error(e)
except (Exception2, Exception3) as e:
logging.error(e)
-
raise Keyword
if(condition):
raise Exception('Something went wrong')
-
Exception Chains
try:
...
except Exception as e:
raise Exception2('Something went wrong')
-
Built-in Exceptions
-
Persistence
Files
-
The open Built-in Function
Use the open built-in function to acquire a file object.
fout = open('new_file', 'w')
fout.write("Hello file\n")
# 11
fout.close()
fin = open('new_file', 'r')
for line in fin:
print(line)
# Hello file
fout.close()
-
Available Modes
character | Meaning |
---|---|
r | open for reading (default) |
w | open for writing, truncating the file first |
x | open for exclusive creation, failing if the file already exists |
a | open for writing, appending to the end of the file if it exists |
b | binary mode |
t | text mode (default) |
+ | open a disk file for updating (reading and writing) |
-
The with Statement
The with statement is used to wrap the execution of a block with methods defined by a context manager.
The context manager will automatically close the file handler when you are done with it.
-
Opening Files Using With Statement
with open('new_file', 'r') as file_handler:
for line in file_handler:
print(line)