reading-notes

My reading notes journal for Code Fellows

View the Project on GitHub

Readings: FileIO & Exceptions

Read & Write Files in Python

This topic matters because reading and writing files is extremely important and you’ll spend a lot of your career doing this.

What Is a File?

Before we can go into how to work with files in Python, it’s important to understand what exactly a file is and how modern operating systems handle some of their aspects.

At its core, 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. In the end, these byte files are then translated into binary 1 and 0 for easier processing by the computer.

Files on most modern file systems are composed of three main parts:

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 The file format with the header on top, data contents in the middle and the footer on the bottom. What this data represents depends on the format specification used, which is typically represented by an extension. For example, a file that has an extension of .gif most likely conforms to the Graphics Interchange Format specification. There are hundreds, if not thousands, of file extensions out there. For this tutorial, you’ll only deal with .txt or .csv file extensions.

Text File Types A text file is the most common file that you’ll encounter. Here are some examples of how these files are opened:

open(‘abc.txt’)

open(‘abc.txt’, ‘r’)

open(‘abc.txt’, ‘w’)

This is cited from https://realpython.com/read-write-files-python/

Exceptions in Python

Syntax errors occur when the parser detects an incorrect statement. Observe the following example:

print( 0 / 0 )) File “", line 1 print( 0 / 0 )) ^ SyntaxError: invalid syntax The arrow indicates where the parser ran into the syntax error. In this example, there was one bracket too many. Remove it and run your code again:

print( 0 / 0) Traceback (most recent call last): File “", line 1, in ZeroDivisionError: integer division or modulo by zero This time, you ran into an exception error. This type of error occurs whenever syntactically correct Python code results in an error. The last line of the message indicated what type of exception error you ran into.

Instead of showing the message exception error, Python details what type of exception error was encountered. In this case, it was a ZeroDivisionError. Python comes with various built-in exceptions as well as the possibility to create self-defined exceptions.

We can use raise to throw an exception if a condition occurs. The statement can be complemented with a custom exception.

Illustration of raise statement usage If you want to throw an error when a certain condition occurs using raise, you could go about it like this:

x = 10 if x > 5: raise Exception(‘x should not exceed 5. The value of x was: {}’.format(x)) When you run this code, the output will be the following:

Traceback (most recent call last): File “”, line 4, in Exception: x should not exceed 5. The value of x was: 10 The program comes to a halt and displays our exception to screen, offering clues about what went wrong.

This is cited from https://realpython.com/python-exceptions/

REFERENCES

https://realpython.com/courses/reading-and-writing-files-python/ https://realpython.com/python-exceptions/