Table of Contents
When you are dealing with files and directories or operating system version and environment variables the os module needs to become your friend
Get current working directory
import os print(os.getcwd())
Get platform info
os.name == "nt" # Windows os.name == "posix" # Linux and macOS
Get environment variables
assert os.environ.get("FOO") == "BAR"
Check if file is a directory
Checking if a file exists
Concatenating file paths
Removing base path
import os full_path = '/book/html/foo/bar/' base_path = '/book/html' assert os.path.relpath(full_path, base_path) == '/foo/bar'
splitted = os.path.split('/book/html/foo/bar') assert splitted == ("/book/html/foo", "bar")