Öffnen, lesen, schreiben

with open(filename, "r+") as file:
	if "search pattern" in file.read():
		return file
 
with open(filename, "a") as file:
	file.write("Append this.")
 
with open(filename, "w") as file:
	file.writelines("This is written to the file.")

Working with pathlib

import Path from pathlib
 
# Two parent directories up from this one
two_dirs_up = Path(__file__).resolve().parent.parent
 
path = Path.home() / "folder" 
# This actually results in "$HOME/folder". I mean... wha-?

Working with os

import os
 
os.path.join(path, name)
 
os.listdir(path) # Top level dir
os.walk(path) # Dir and all subdirs
 
os.path.isfile(file)
os.path.isdir(directory)

Tips and tricks

is_markdown = filename.endswith(".md")

Ressourcen