We use operating system paths to find and organize files and folders on a computer. Working with paths helps your program find files no matter where it runs.
Working with operating system paths in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
import os # Join parts of a path full_path = os.path.join('folder', 'subfolder', 'file.txt') # Get folder name from path folder = os.path.dirname(full_path) # Get file name from path file = os.path.basename(full_path) # Check if path exists exists = os.path.exists(full_path)
Use os.path.join() to combine parts safely, so it works on Windows and Mac/Linux.
os.path has many useful functions to work with paths without errors.
Examples
Python
import os path = os.path.join('home', 'user', 'notes.txt') print(path)
Python
import os path = '/home/user/notes.txt' folder = os.path.dirname(path) print(folder)
Python
import os path = '/home/user/notes.txt' file = os.path.basename(path) print(file)
Python
import os path = 'somefile.txt' print(os.path.exists(path))
Sample Program
This program shows how to join parts into a path, get folder and file names, and check if the path exists.
Python
import os folder = 'documents' subfolder = 'projects' filename = 'report.txt' # Create full path full_path = os.path.join(folder, subfolder, filename) print(f'Full path: {full_path}') # Get folder and file parts folder_part = os.path.dirname(full_path) file_part = os.path.basename(full_path) print(f'Folder part: {folder_part}') print(f'File part: {file_part}') # Check if path exists exists = os.path.exists(full_path) print(f'Does the path exist? {exists}')
Important Notes
Paths look different on Windows (using backslashes) and Mac/Linux (using slashes), but os.path.join() handles this for you.
Always use os.path functions instead of string operations to avoid mistakes.
Summary
Use os.path.join() to build paths safely.
Use os.path.dirname() and os.path.basename() to get folder and file names.
Use os.path.exists() to check if a path is real on the computer.
Practice
1. Which function is used to safely combine folder and file names into a full path in Python?
easy
Solution
Step 1: Understand purpose of os.path.join()
It combines parts of a path into one full path, handling separators correctly.Step 2: Compare with other functions
os.path.exists() checks if a path exists, basename() gets file name, dirname() gets folder name.Final Answer:
os.path.join() -> Option AQuick Check:
Combine paths = os.path.join() [OK]
Hint: Use os.path.join() to build paths safely [OK]
Common Mistakes:
- Confusing join() with exists()
- Using basename() to join paths
- Using dirname() to combine paths
2. Which of the following is the correct syntax to get the folder name from a path stored in variable
path?easy
Solution
Step 1: Identify function to get folder name
os.path.dirname(path) returns the directory part of the path.Step 2: Check other options
basename() returns file name, join() combines paths, exists() checks path existence.Final Answer:
os.path.dirname(path) -> Option BQuick Check:
Folder name = os.path.dirname(path) [OK]
Hint: Use os.path.dirname() to get folder name from path [OK]
Common Mistakes:
- Using basename() to get folder
- Calling join() with one argument
- Confusing exists() with dirname()
3. What will be the output of this code?
import os
path = os.path.join('folder', 'subfolder', 'file.txt')
print(os.path.basename(path))medium
Solution
Step 1: Understand os.path.join()
It creates 'folder/subfolder/file.txt' (with correct separator).Step 2: Understand os.path.basename()
It returns the last part of the path, which is the file name 'file.txt'.Final Answer:
file.txt -> Option CQuick Check:
basename() returns file name [OK]
Hint: basename() returns last part (file) of path [OK]
Common Mistakes:
- Expecting folder name instead of file
- Confusing join() output with basename()
- Printing full path instead of basename
4. What is wrong with this code snippet?
import os
path = os.path.join('folder', 'file.txt')
if os.path.exists:
print('Path exists')medium
Solution
Step 1: Check usage of os.path.exists
It is a function and must be called with parentheses and argument: os.path.exists(path).Step 2: Verify other parts
join() usage is correct, print() has parentheses, path is defined.Final Answer:
os.path.exists is used without parentheses -> Option AQuick Check:
Call exists() with parentheses [OK]
Hint: Call os.path.exists() with path argument [OK]
Common Mistakes:
- Forgetting () after exists
- Passing no argument to exists()
- Misusing join() function
5. You want to check if a file named
data.csv exists inside a folder reports located in the user's home directory. Which code correctly builds the path and checks existence?hard
Solution
Step 1: Get user's home directory
Use os.path.expanduser('~') to get the home folder path.Step 2: Join home, reports, and file name
Use os.path.join() to combine home path, 'reports', and 'data.csv'.Step 3: Check if the full path exists
Pass the full joined path to os.path.exists() to check existence.Final Answer:
os.path.exists(os.path.join(os.path.expanduser('~'), 'reports', 'data.csv')) -> Option DQuick Check:
Expand user + join + exists = os.path.exists(os.path.join(os.path.expanduser('~'), 'reports', 'data.csv')) [OK]
Hint: Use expanduser('~') to get home folder before joining [OK]
Common Mistakes:
- Using relative path without home folder
- Using literal '~/...' without expanduser
- Using current working directory instead of home
