FileIO & Exceptions
This topic matters as it relates to learning proper file manipulation and exceptions in python.
Read & Write Files in Python
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.
Main parts
- Header: Metadata about the contents of the file.
- 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
File path is a string that represents the location of a file.
Main parts
- 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.
Opening and Closing a File in Python
Open a file
We do this by invoking the open()
built-in function. open()
has a single required argument that is the path to the file. open()
has a single return. (e.g.)
file = open('dog_breeds.txt')
Close a file
One way to close a file is by using a try-finally
block (e.g.)
reader = open('dog_breeds.txt')
try:
# Further file processing goes here
finally:
reader.close()
Another way to close a file is by using a with statement
(e.g.)
with open('dog_breeds.txt') as reader:
# Further file processing goes here
Common mode options:
r
Open for reading (default).w
Open for writing, truncating (overwriting) the file first.rb
orwb
Open in binary mode (read/write using byte data).
Reading and Writing Opened Files
Read
We use the following methods:
.read(size=-1)
This reads from the file based on the number of size bytes. If no argument is passed or None or -1 is passed, then the entire file is read..readline(size=-1)
This reads at most size number of characters from the line. This continues to the end of the line and then wraps back around. If no argument is passed or None or -1 is passed, then the entire line (or rest of the line) is read..readlines()
This reads the remaining lines from the file object and returns them as a list.
Write
We use the following methods:
.write(string)
This writes the string to the file..writelines(seq)
This writes the sequence to the file. No line endings are appended to each sequence item. It’s up to you to add the appropriate line ending(s).
Exceptions in Python
Exceptions versus Syntax Errors
Syntax errors: Occur when the parser detects an incorrect statement. (e.g.)
>>> print( 0 / 0 ))
File "<stdin>", line 1
print( 0 / 0 ))
^
SyntaxError: invalid syntax
Exception errors: Occur whenever syntactically correct Python code results in an error. (e.g.)
>>> print( 0 / 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
raise
Allows us to throw an exception at any time.assert
Enables us to verify if a certain condition is met and throw an exception if it isn’t.try
All statements are executed until an exception is encountered.except
is used to catch and handle the exception(s) that are encountered in thetry
clause.else
Lets us code sections that should run only when no exceptions are encountered in thetry
clause.finally
Enables us to execute sections of code that should always run, with or without any previously encountered exceptions.
Things I want to know more about
- I would like to know more about other options for modes to work with files and how to implement exceptions efficiently.