Skip to content

PATH JOINING - Using os.path.join()

Python
#!/usr/bin/env python3
"""
PATH JOINING - Using os.path.join()
Demonstrates portable path construction
"""

import os
import tempfile

print("=" * 60)
print("PATH JOINING - os.path.join()")
print("=" * 60)

# Example 1: Basic join
print("\n1. Basic Path Joining")
print("-" * 40)
path = os.path.join("dir1", "dir2", "file.txt")
print(f"  Joined: {path}")

# Example 2: Multiple components
print("\n2. Join Multiple Components")
print("-" * 40)
components = ["home", "user", "documents", "file.pdf"]
full_path = os.path.join(*components)
print(f"  Components: {components}")
print(f"  Joined: {full_path}")

# Example 3: With absolute path
print("\n3. Join with Absolute Base")
print("-" * 40)
base = tempfile.gettempdir()
filename = "test.txt"
full_path = os.path.join(base, filename)
print(f"  Base: {base}")
print(f"  File: {filename}")
print(f"  Full: {full_path}")

# Example 4: Build directory structure paths
print("\n4. Build Directory Structure")
print("-" * 40)
base_dir = "/project"
paths = [
    os.path.join(base_dir, "src", "main.py"),
    os.path.join(base_dir, "tests", "test_main.py"),
    os.path.join(base_dir, "docs", "README.md")
]
for p in paths:
    print(f"  {p}")

# Example 5: Platform-independent
print("\n5. Platform-Independent Paths")
print("-" * 40)
print(f"  OS separator: '{os.sep}'")
path1 = os.path.join("folder", "subfolder", "file.txt")
print(f"  Joined path: {path1}")

# Example 6: Join with current directory
print("\n6. Join with Current Directory")
print("-" * 40)
cwd = os.getcwd()
filename = "data.txt"
full_path = os.path.join(cwd, filename)
print(f"  CWD: {cwd}")
print(f"  File: {filename}")
print(f"  Full: {full_path}")

# Example 7: Empty strings
print("\n7. Handle Empty Strings")
print("-" * 40)
path1 = os.path.join("", "file.txt")
path2 = os.path.join("dir", "", "file.txt")
print(f"  join('', 'file.txt'): {path1}")
print(f"  join('dir', '', 'file.txt'): {path2}")

# Example 8: Absolute path override
print("\n8. Absolute Path in Middle")
print("-" * 40)
path = os.path.join("dir1", "/absolute", "file.txt")
print(f"  join('dir1', '/absolute', 'file.txt'): {path}")
print("  Absolute path discards previous components")

# Example 9: Build file paths in loop
print("\n9. Build Multiple File Paths")
print("-" * 40)
base = tempfile.gettempdir()
files = ["file1.txt", "file2.txt", "file3.txt"]
for f in files:
    full_path = os.path.join(base, f)
    print(f"  {f} -> {full_path}")

# Example 10: Join with variables
print("\n10. Dynamic Path Construction")
print("-" * 40)
project = "myproject"
version = "v1.0"
filename = "release.zip"
path = os.path.join("/releases", project, version, filename)
print(f"  Project: {project}")
print(f"  Version: {version}")
print(f"  Path: {path}")

print("\n" + "=" * 60)
print("Key Points:")
print("  - os.path.join() creates platform-independent paths")
print("  - Uses correct separator for OS (/ or \\)")
print("  - Handles multiple path components")
print("  - Absolute path discards previous parts")
print("  - Always use instead of string concatenation")
print("=" * 60)