os.path.join() / os.path.exists() / os.path.basename()
Functions from the os.path module for building file paths and extracting their components. It handles OS differences (Windows/Mac/Linux) so you can manipulate paths safely across platforms.
Syntax
import os # Joins path components together. os.path.join(path1, path2, ...) # Checks whether a file or directory exists. os.path.exists(path) # Returns the last component of a path (the filename). os.path.basename(path) # Returns the directory portion of a path. os.path.dirname(path) # Splits a path into the filename and extension. os.path.splitext(path) # Converts a path to an absolute path. os.path.abspath(path)
Function List
| Function | Description |
|---|---|
| os.path.join(p1, p2, ...) | Joins multiple path components into a single path string, using the OS-specific separator (/ or \) automatically. |
| os.path.exists(path) | Returns True if a file or directory exists at the specified path. |
| os.path.basename(path) | Returns the last component of a path (the filename or directory name). |
| os.path.dirname(path) | Returns the directory portion of a path. |
| os.path.splitext(path) | Splits a path into a tuple of (filename, extension) and returns it. |
| os.path.abspath(path) | Converts a relative path to an absolute path and returns it. |
| os.path.isfile(path) | Returns True if the path points to an existing file. |
| os.path.isdir(path) | Returns True if the path points to an existing directory. |
Sample Code
import os
# Use join() to build a path safely.
base_dir = '/Users/user/documents'
filename = 'report.txt'
full_path = os.path.join(base_dir, 'reports', filename)
print(full_path) # Outputs: /Users/user/documents/reports/report.txt
# Use exists() to check whether a file exists.
print(os.path.exists('/Users/user/documents')) # Outputs True if it exists.
print(os.path.exists('/nonexistent/path')) # Outputs False.
# Use basename() and dirname() to decompose a path.
path = '/var/www/html/index.php'
print(os.path.basename(path)) # Outputs: index.php
print(os.path.dirname(path)) # Outputs: /var/www/html
# Use splitext() to extract the file extension.
name, ext = os.path.splitext('photo.jpg')
print(name) # Outputs: photo
print(ext) # Outputs: .jpg
# Generate a new filename with a different extension.
src = 'document.txt'
dst = os.path.splitext(src)[0] + '.md'
print(dst) # Outputs: document.md
# Use abspath() to get the absolute path.
print(os.path.abspath('.')) # Outputs the absolute path of the current directory.
print(os.path.abspath('../data')) # Converts the relative path to an absolute path.
# Use isfile() and isdir() to check the type.
print(os.path.isfile('/etc/hosts')) # Outputs True if it is a file.
print(os.path.isdir('/etc')) # Outputs True if it is a directory.
Notes
os.path.join() uses the OS-appropriate separator (backslash \ on Windows, forward slash / on Mac/Linux) to join paths. Building paths with string concatenation (the + operator) can break across environments, so always use os.path.join() for safe, portable code.
Since Python 3.4, the pathlib module provides the same functionality in a more object-oriented style. New code is encouraged to use pathlib.Path. That said, os.path remains widely used for compatibility with existing code and third-party libraries.
For modern path handling, see pathlib.Path(). For directory operations, see os.listdir() / os.makedirs() / os.remove().
If you find any errors or copyright issues, please contact us.