Skip to content

Latest commit

 

History

History
76 lines (49 loc) · 3.34 KB

401-03.md

File metadata and controls

76 lines (49 loc) · 3.34 KB

Home

Doc: 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. These byte files are translated into binary 1 and 0 for easier processing by the computer.

Three parts of a file:

  1. Header: metadata about the contents of the file (file name, size, type, and so on)
  2. Data: contents of the file as written by the creator or editor
  3. End of file (EOF): special character that indicates the end of the file

Three parts of a file path:

  1. Folder Path: the file folder location on the file system, separated by a forward slash / (Unix) or backslash \ (Windows)
  2. File Name: actual name of the file
  3. Extension: the end of the file path starting with a period such as .png - used to indicate the file type

The double-dot (..) can be chained together to traverse multiple directories above the current directory.

Line endings should use the sequence of the Carriage Return (CR or \r) and the Line Feed (LF or \n) characters (CR+LF or \r\n). Windows uses the CR+LF characters to indicate a new line, while Unix and the newer Mac versions use just the LF character.

Character Encodings

An encoding is a translation from byte data to human readable characters. Typically done by assigning a numerical value to represent a character. The two most common encodings are the ASCII and UNICODE Formats.

Opening and Closing a File in Python

Opening
file = open('dog_breeds.txt')

Closing Using a try-finally block.
try:
# Further file processing goes here
finally:
reader.close()

Or using a with statement.
with open('dog_breeds.txt') as reader:
# Further file processing goes here

You may also want to use a second positional argument, mode. The default and most common is 'r'.

with open('dog_breeds.txt', 'r') as reader:
# Further file processing goes here

Doc: Other Modes

Text File Types

A text file is the most common file that you’ll encounter.
A buffered binary file type is used for reading and writing binary files.
A raw file type is not typically used - “generally used as a low-level building-block for binary and text streams.” Source

Python Exceptions

Doc: An Introduction

A Python program terminates as soon as it encounters an error. In Python, an error can be a syntax error or an exception.

Syntax errors occur when the parser detects an incorrect statement.

Exception errors occur whenever syntactically correct Python code results in an error.

raise allows you to throw/force an exception at any time.
assert enables you to verify if a certain condition is met and throw an exception if it isn’t.
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.

Home