home ~ projects ~ socials

Join Strings In Python To Make A Path

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

import os.path

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

print(joined_path)
Output:
a/b/c



This is useful in when you need a user's
home directory:
import os.path
from pathlib import Path

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

print(joined_path)
Output:
/Users/alan/a/b/c
-- end of line --