Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Join Strings In Python To Make A Path

This is the basic way to join strings into a file or directory path in python:

Code

import os.path

joined_path = os.path.join('a', 'b', 'c')

print(joined_path)

Results

a/b/c



This is useful in when you need a user's
home directory:

Code

import os.path
from pathlib import Path

joined_path = os.path.join(Path.home(), 'a', 'b', 'c')

print(joined_path)

Results

/Users/alan/a/b/c

Mushing directories together to make a Python path