Readings: FileIO & Exceptions
Reading and Writing Files in Python
What is a file?
- At its core, a file is a contiguous set of bytes used to store data.
- Header: metadata about the contents of the file (file name, size, type, and so on)
- Data: contents of the file as written by the creator or editor
- End of file (EOF): special character that indicates the end of the file
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 Name: the actual name of the file
- Extension: the end of the file path pre-pended with a period (.) used to indicate the file type
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
- This can make iterating over each line problematic, and you may need to account for situations like this.
Character encodings
- It’s important to note that parsing a file with the incorrect character encoding can lead to failures or misrepresentation of the character.
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”
- “the pathname of the file from which the module was loaded, if it was loaded from a file.”
Python Exceptions: An Introduction
Exceptions versus Syntax Errors
- Syntax errors occur when the parser detects an incorrect statement.
- Exception error occurs whenever syntactically correct Python code results in an error.
The AssertionError Exception
- Instead of waiting for a program to crash midway, you can also start by making an assertion in Python.
The try and except Block: Handling Exceptions
- The try and except block in Python is used to catch and handle exceptions.
try: with open('file.log') as file: read_data = file.read() except FileNotFoundError as fnf_error: print(fnf_error)
The else Clause
- using the else statement, you can instruct a program to execute a certain block of code only in the absence of exceptions
Cleaning Up After Using finally
- Imagine that you always had to implement some sort of action to clean up after executing your code
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 :( )
- Opening and Closing Files
- Reading From a File
- Writing to a File
- Appending to a File
- File Locations and Paths
- What Is a File?
- Examining File Contents