reading-notes

reading notes for code fellows


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

FileIO and Exceptions

These topics are important because it is essential that we should be able to interact with files and be able to put proper error handling into our programs.

Read and Write Files in Python

A file is a set of bytes used to store data, which is organized in a specific format and translated into binary for the computer. Files on most modern systems are composed of a header, which contains metasata about the file contents, data, the contents of the file, and the end of file (EOF), which is a special character indicating the end of the file.

A file path is used to access a file on an OS, which is also made up of three major parts; a folder path, the folder location on the file system, a file name, the name of the file, and an extension, the end of the file path beginning with a period and denoting the file type.

The different line ending characters are Carriage Return (CR) \r and Line Feed (LF) \n. Different operating systems will sometimes use different combinations of these characters, leading to issues when dealing with a file that originated in another operating system.

When you want to open a file in python, you use the open('filename.txt', 'mode') function, which will return the file object in the specified mode (ex, r: read-only). After you are done with a file, it is important to close it using var_name.close(). A good way to ensure a file closes even if an error is thrown is to use a try-finally block, or when you open it you can use with open('filename.txt') as var_name, which will automatically close the file when it leaves the with block.

There are three different categories of files:

when you want to read and open file, you can use the commands

to write to a file, you can use the commands

if you don’t want to overwrite the file and only want to append to it, use mode a

Exceptions in Python

Python programs will terminate as soon as they reacg a syntax error (which occurs when the parser detects an incorrect statement) or an exception error (when syntactically correct code results in an error). You can use raise to throw an exception if a certain condition occurs with a custom statement. Instead of waiting on a program to crash, you can put assertions in the code which will allow the code to continue if they evaluate to true, or throw an AssertionError exception if its false. If you want your code to continue running after hitting a specific error, you can place the area that would throw the exeption within a try-except block, which will attempt the code in try and, if that should fail, will run the code in except. If you add an else statement after, it will only run if the try block succeeded. Adding a finally block at the end will run the code within regardless of anything else that ran previously.

Things I Want to Know More About

All of the different preset exceptions that are in python

Discussion

Reading and writing to files in Python is relatively similar to the way it was in javascript, what with assigning the opened file to a variable and manipulating that variable as necessary to complete the tasks you have with the file itself. Also, the try-except blocks are similar to javascript’s try-catch blocks, but seemingly with a bit more functionality since we can add on elses and finallys.

[< table of contents]

[< home]