Table of Contents
Motivation
When you are dealing with files and directories or operating system version and environment variables the os module needs to become your friend
os
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"
os.path
Check if file is a directory
os.path.isdir("../os_path")
Checking if a file exists
os.path.exists(file_name)
Concatenating file paths
os.path.join(path_1, path_2)
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'
Splitting paths
splitted = os.path.split('/book/html/foo/bar')
assert splitted == ("/book/html/foo", "bar")