reading-notes

This Repo required for Asac labs class 2


Project maintained by ManarAbdelkarim Hosted on GitHub Pages — Theme by mattgraham

# FileIO & Exceptions

What is a file ?

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.

Parts of the file :

What is a File Paths

Folder Path: the file folder location on the file system where subsequent folders are separated by a forward slash / (Unix) or backslash \ (Windows)

File Paths Parts :

Opening and Closing a File in Python

  1. Open : This is done by invoking the open() built-in function.
  2. close : This is done by invoking the close() built-in function.

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()

options for file mode :

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)

Python Exceptions

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)

Raising an exception

raise allows you to throw an exception at any time.

The AssertionError Exception

assert enables you to verify if a certain condition is met and throw an exception if it isn’t.

The try and except Block: Handling Exceptions

The else 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

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