This Repo required for Asac labs class 2
# FileIO & Exceptions
a file is a contiguous set of bytes used to store data. This data is organized in a specific format and can be anything as simple as a text file or as complicated as a program executable.
Folder Path: the file folder location on the file system where subsequent folders are separated by a forward slash / (Unix) or backslash \ (Windows)
Example:
reader = open(‘dog_breeds.txt’)
#or you can use => with open(‘dog_breeds.txt’) as reader:
try:
# Further file processing goes here finally: reader.close()
Character# | Meaning |
---|---|
‘r’ | ’’ Open for reading (default) |
‘w’ | Open for writing, truncating (overwriting) the file first |
‘rb’ or ‘wb’ | Open in binary mode (read/write using byte data) |
Expections happen when the python code is syntactically correct but u run into an error (the last line of the error tells you what type of exception it is)
raise allows you to throw an exception at any time.
assert enables you to verify if a certain condition is met and throw an exception if it isn’t.
In the try clause, all statements are executed until an exception is encountered.
except is used to catch and handle the exception(s) that are encountered in the try clause.
In Python, using the else statement, you can instruct a program to execute a certain block of code only in the absence of exceptions. else lets you code sections that should run only when no exceptions are encountered in the try clause.
finally enables you to execute sections of code that should always run, with or without any previously encountered exceptions. Everything in the finally clause will be executed it doesn’t matter if u run into an exception