reading-notes

Software Development Reading Notes

View on GitHub

Readings: FileIO & Exceptions

Reading and Writing Files in Python

What is a file?

File paths

The double-dot (..) can be chained together to traverse multiple directories above 
the current directory. For example,to access animals.csv from the to folder, 
you would use ../../animals.csv.

Line endings

Opening and Closing a File in Python

file = open('dog_breeds.txt')
reader = open('dog_breeds.txt')
try:
    # Further file processing goes here
finally:
    reader.close()

file

Python Exceptions: An Introduction

Exceptions versus Syntax Errors

The AssertionError Exception

The try and except Block: Handling Exceptions

The else Clause

Cleaning Up After Using finally

Summary

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.

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.

Reading and Writing Files in Python (member only content :( )