Module 16
Files, Pickling, CSV, JSON, and OS Operations
Learn file handling and common data exchange formats.
01
Files and Streams — Data Persistency in Python
Learn why Python files exist: data persistency, the stream between memory and disk, and how text files differ from binary files.
02
File Opening Modes — r, w, a, r+, w+, a+, x, and x+
Learn each of the eight Python file modes — r, w, a, r+, w+, a+, x, and x+ — including overwrite, append, and exclusive create.
03
Opening Files — open() and with open() as
Open a Python file with open() or with open(), and close it manually or automatically so the saved record stays consistent.
04
Writing Files — write() and writelines()
Write text into a Python file with write() and writelines(), and convert numbers to strings before they are stored as characters.
05
Reading Files — read() and readlines()
Read a Python text file with read() into one string, or with readlines() into a list of lines, and handle a missing file.
06
Random Access — tell() and seek() on a File Pointer
Move inside a Python file with tell() and seek(), read a slice with read(n), and jump the pointer back to any character index.
07
Binary Files — Read and Write Bytes with Mode b
Open Python files in binary mode with b, copy bytes with read and write, and keep images and other media out of text mode.
08
Pickling and Unpickling — pickle.dump() and pickle.load()
Pickle one whole Python object into a binary file with dump(), then restore that same object in a single read with load().
09
CSV Files — csv.writer and csv.reader
Store tabular data rows in a Python CSV file using csv.writer, then read each comma-separated record back using csv.reader.
10
CSV Dicts — DictWriter and DictReader
Write dictionary rows to a CSV file with DictWriter and writeheader(), then read each row back as a dict with DictReader.
11
JSON Files — loads, dumps, load, and dump
Convert between Python dicts and JSON text with loads and dumps, and move dicts to and from .json files with load and dump.
12
OS Operations — Folders and Files with the os Module
Create, list, rename, and remove files and folders in Python with mkdir, makedirs, listdir, rename, remove, rmdir, and removedirs.