5.1 File Input/Output Operations
File Opening Modes
Modes are specified as a string combining read (r), write (w), append (a), exclusive creation (x), binary (b), text (t), and update (+). Default is rt (read text).
| Mode | Description |
|---|---|
r |
Read (default). Fails if file doesn't exist. |
w |
Write. Truncates file or creates new. |
a |
Append. Writes to end; creates if not exists. |
x |
Exclusive creation. Fails if file exists. |
b |
Binary mode (e.g., rb, wb). |
t |
Text mode (default). |
+ |
Update (read/write). E.g., r+, w+. |
Reading Methods
-
read(size=-1): Reads entire file orsizebytes/characters. -
readline(): Reads one line including trailing newline. -
readlines(): Returns list of all lines (includes newlines).
Writing Methods
-
write(str): Writes a string to file. -
writelines(iterable): Writes each string from iterable (no automatic newlines).
Context Manager (with statement)
Automatically closes file after block, even on exceptions.
with open('file.txt', 'r') as f:
data = f.read()
# File closed here
[!TIP] Always use
withfor file operations to prevent resource leaks.
CSV Files (csv module)
import csv
# Reading
with open('data.csv') as f:
reader = csv.reader(f) # Returns iterator of lists
for row in reader: ...
# Writing
with open('out.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['col1', 'col2'])
JSON Files (json module)
-
json.load(file): Deserialize JSON from file to Python object. -
json.dump(obj, file): Serialize Python object to JSON file. -
json.loads(str)/json.dumps(obj): For strings.
File/Directory Manipulation (os, os.path)
-
os.path.exists(path),os.path.isfile(),os.path.isdir() -
os.listdir(dir): List directory contents. -
os.mkdir(path),os.makedirs(path, exist_ok=True) -
os.remove(path),os.rmdir(path) -
os.path.join(*parts): Platform-independent path joining.
5.2 Exception Handling
Basic Syntax
try:
# risky code
except SomeError as e:
# handle error, e is exception instance
else:
# runs if no exception
finally:
# always runs (cleanup)
Handling Multiple Exceptions
except (TypeError, ValueError) as e: # Tuple of exceptions
...
# Or separate blocks
except TypeError: ...
except ValueError: ...
Raising Exceptions
raise ValueError("Invalid input")
# Re-raising current exception in except block
except:
raise
Custom Exceptions
class MyError(Exception):
def __init__(self, message):
super().__init__(message)
Exception Chaining
Preserves original exception context.
try:
...
except IOError as e:
raise RuntimeError("Failed processing") from e
[!TIP] Use
from Noneto suppress chaining:raise NewError() from None.
5.3 Modules and Packages
Importing
| Statement | Effect |
|---|---|
import module |
Imports whole module; access via module.name |
from module import name |
Imports specific name into current namespace |
import module as alias |
Imports with alias |
Module Search Path
sys.path list of directories Python searches. Current directory first, then PYTHONPATH, then installation defaults.
__name__ Guard
if __name__ == "__main__":
# Code runs only when script executed directly, not when imported
[!TIP] This is boxed because it’s essential for reusable modules.
Packages
-
Directory with
__init__.py(can be empty). -
Enables hierarchical imports:
from package.sub import module. -
Absolute imports: Use full path from project root.
-
Relative imports: Within package, use
.(current) or..(parent).from .sibling import func # relative from package.sibling import func # absolute
Installing Packages
pip install package_name
pip install -r requirements.txt # from file
5.4 Standard Library Highlights
os & os.path
-
os.environ: Dictionary-like environment variables. -
os.getcwd(),os.chdir(path) -
os.pathfunctions:abspath(),basename(),dirname(),splitext(),normpath().
sys
-
sys.argv: List of command-line arguments ([0]is script name). -
sys.exit([arg]): Exit interpreter. -
sys.version,sys.platform,sys.path.
datetime
from datetime import date, time, datetime, timedelta
today = date.today()
now = datetime.now()
delta = timedelta(days=7)
future = now + delta
# Formatting
now.strftime("%Y-%m-%d %H:%M:%S")
math & random
-
math:pi,e,sqrt(),ceil(),floor(),sin(),log(). -
random:random(),randint(a,b),choice(seq),shuffle(seq).
collections
| Class | Purpose |
|---|---|
namedtuple(typename, fields) |
Tuple with named fields |
deque([iterable]) |
Fast appends/pops from both ends |
Counter([iterable]) |
Dict subclass for counting hashables |
defaultdict(default_factory) |
Dict with default for missing keys |
OrderedDict() |
Dict that remembers insertion order (Python 3.7+ dict does too) |
itertools
-
count(start=0, step=1): Infinite sequence. -
cycle(iterable): Repeats indefinitely. -
repeat(obj, times=None): Repeats object. -
chain(*iterables): Chains iterables sequentially. -
combinations(iterable, r): All r-length combinations.
functools
-
partial(func, *args, **kwargs): Fixes arguments, returns new function. -
lru_cache(maxsize=128): Decorator for memoization (caches results). -
wraps(wrapped): Preserves metadata of wrapped function in decorators.
5.5 Data Serialization and Persistence
JSON
-
Human-readable, language-agnostic.
-
Supports:
dict,list,str,int,float,True/False,None. -
json.dump(obj, file, indent=4)for pretty printing. -
Custom encoding via
defaultparameter (function that returns serializable object).
pickle
-
Serializes arbitrary Python objects (including classes, functions).
-
Security Risk: Unpickling untrusted data can execute arbitrary code. Never unpickle from untrusted sources.
-
pickle.dump(obj, file),pickle.load(file).
shelve
- Persistent, dictionary-like object storage using
pickleunder the hood.
import shelve
with shelve.open('mydata') as db:
db['key'] = {'a': 1} # Any picklable object
value = db['key']
[!TIP]
shelvekeys must be strings; values are pickled automatically.
5.6 Advanced Topics (Context Managers & Decorators)
Context Managers
Protocol: Define __enter__(self) and __exit__(self, exc_type, exc_val, exc_tb).
class ManagedFile:
def __init__(self, filename):
self.filename = filename
def __enter__(self):
self.file = open(self.filename, 'r')
return self.file
def __exit__(self, exc_type, exc_val, exc_tb):
if self.file:
self.file.close()
# Return True to suppress exception
contextlib.contextmanager
Decorator for generator-based context managers.
from contextlib import contextmanager
@contextmanager
def managed_file(filename):
f = open(filename, 'r')
try:
yield f
finally:
f.close()
Decorators
Wrap functions to modify behavior.
def decorator(func):
def wrapper(*args, **kwargs):
# pre-processing
result = func(*args, **kwargs)
# post-processing
return result
return wrapper
@decorator
def my_func(): ...
Decorators with Arguments
def decorator_with_args(arg):
def decorator(func):
def wrapper(*args, **kwargs):
# use arg here
return func(*args, **kwargs)
return wrapper
return decorator
@decorator_with_args("some_value")
def my_func(): ...
functools.wraps
Preserves original function’s __name__, __doc__, etc.
from functools import wraps
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
Class Method Decorators
-
@staticmethod: Noselforclspassed. -
@classmethod: First argument iscls(class itself).
5.7 Logging and Configuration
Logging Levels (increasing severity)
| Level | Numeric Value | When to Use |
|---|---|---|
DEBUG |
10 | Diagnostic details |
INFO |
20 | Confirmation things work |
WARNING |
30 | Something unexpected |
ERROR |
40 | Serious problem |
CRITICAL |
50 | Severe error, may terminate |
Basic Configuration
import logging
logging.basicConfig(
level=logging.DEBUG,
filename='app.log', # Log to file
filemode='w', # Overwrite ('a' to append)
format='%(asctime)s - %(levelname)s - %(message)s'
)
Logging Usage
logging.debug("Details")
logging.info("Update")
logging.warning("Issue")
logging.error("Failure")
logging.critical("System down")
[!TIP] Set level to
logging.INFOor higher in production to avoid verbose logs.
5.8 Command-Line Argument Parsing
sys.argv
Simple list of arguments: sys.argv[0] is script name, sys.argv[1:] are user args.
Manual parsing required (prone to errors).
argparse Module
import argparse
parser = argparse.ArgumentParser(description="Description")
# Positional
parser.add_argument('input_file', help='Path to input')
# Optional
parser.add_argument('-o', '--output', help='Output path')
parser.add_argument('--verbose', action='store_true') # Flag
parser.add_argument('--count', type=int, default=1) # Type validation
args = parser.parse_args()
print(args.input_file, args.output)
-
Positional: Required, order matters.
-
Optional: Prefixed with
-or--. Useaction='store_true'for flags. -
typeenforces conversion;defaultsets fallback. -
helptext appears in--help.
[!TIP]
argparseautomatically generates-h/--helpwith descriptions.