Reading-Notes


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

Reading and Writing Files in Python

file systems 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

File Paths When you access a file on an operating system, a file path is required. The file path is a string that represents the location of a file. It’s broken up into three major 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.

Encodings The two most common encodings are the ASCII and UNICODE Formats. ASCII can only store 128 characters, while Unicode can contain up to 1,114,112 characters.

ASCII is actually a subset of Unicode (UTF-8), meaning that ASCII and Unicode share the same numerical to character values. It’s important to note that parsing a file with the incorrect character encoding can lead to failures or misrepresentation of the character.

file modes

Opening and Closing a File in Python When you want to work with a file, the first thing to do is to open it. This is done 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, the file object:

file = open(‘dog_breeds.txt’)

opening file

here are some tips and tricks to help you grow your skills.