Tags


Python 3 cheatsheet

First written on July 14, 2020
Last updated on October 8, 2022

Table of contents #

Introduction #

This is a list of instructions I use frequently in:

pathlib #

Files #

DescriptionCodeImports
new filepathlib.Path(shlex.quote(path: str)).touch(0o700, exist_ok=True)import pathlib, shlex
remove filepathlib.Path(shlex.quote(path: str)).unlink(missing_ok=True)import pathlib, shlex
new directorypathlib.Path(shlex.quote(path: str)).mkdir(mode=0o700, parents=True, exist_ok=True)import pathlib, shlex

Paths #

DescriptionCodeImports
get last path component from urlpathlib.Path(urllib.parse.urlsplit(url: str).path).nameimport pathlib, urllib
get relative pathpathlib.Path(shlex.quote(path: str)).nameimport pathlib, shlex
get full pathstr(pathlib.Path(shlex.quote(directory: str), shlex.quote(file: str)))import pathlib, shlex
remove file extensionpathlib.Path(shlex.quote(path: str)).stemimport pathlib, shlex
get file extensionpathlib.Path(shlex.quote(path: str)).suffiximport pathlib, shlex
get the path of a configuration fileconfiguration_file = shlex.quote(sys.argv[1])import shlex, sys

PyYAML #

DescriptionCodeImports
load a YAML fileconfig = yaml.load(open(file: str, 'r'), Loader=yaml.SafeLoader)import yaml

Add or view comments