OS Path module provides functions for working with file and directory paths. It allows to create, modify, validate and retrieve path information in a platform-independent way. These utilities make file and folder operations easier and more reliable across different operating systems.
Importing Module
Before using any os.path function, we need to import the os module.
import os
Explanation: path module is a submodule of the os module, so we access its functions using os.path.
Common OS Path Functions
os.path module provides several useful functions for working with file and directory paths. These functions help extract path information, check path types and normalize paths across different operating systems.
1. os.path.basename(path): returns the final component of a path, usually the file or folder name.
import os
out = os.path.basename("/projects/report.txt")
print(out)
Output
report.txt
Explanation: os.path.basename() extracts the last part of the path and "report.txt" is the final component of the given path.
2. os.path.dirname(path): returns the directory portion of a path.
import os
out = os.path.dirname("/projects/report.txt")
print(out)
Output
/projects
Explanation: os.path.dirname() removes the last component of the path. It returns only the directory path.
3. os.path.isabs(path): checks whether a path is an absolute path.
import os
out = os.path.isabs("/projects/report.txt")
print(out)
Output
True
Explanation: An absolute path starts from the root directory. Since the path begins with /, the function returns True.
4. os.path.isdir(path): checks whether a specified path exists and is a directory.
import os
out = os.path.isdir("C:\\Users")
print(out)
Output
False
Explanation: Returns True if the path exists and points to a directory. Otherwise, it returns False.
5. os.path.isfile(path): checks whether a specified path exists and is a file.
import os
out = os.path.isfile("report.txt")
print(out)
Output
False
Explanation: Returns True if the path exists and points to a file and returns False for directories or non-existent paths.
6. os.path.normcase(path): normalizes the case of a pathname.
import os
out = os.path.normcase("C:/Projects/Report.txt")
print(out)
Output
C:/Projects/Report.txt
Explanation: On Windows, it converts characters to lowercase and changes forward slashes (/) to backslashes (\). On Unix/Linux systems, the path is returned unchanged.
7. os.path.normpath(path): simplifies a path by removing unnecessary separators and references.
import os
out = os.path.normpath("projects/./reports/../report.txt")
print(out)
Output
projects/report.txt
Explanation:
- . refers to the current directory.
- .. refers to the parent directory.
- os.path.normpath() removes these redundant references and returns a cleaner path.
Real-World Example
The following example checks whether a file exists before opening it.
import os
file_path = "report.txt"
if os.path.exists(file_path):
print("File exists")
else:
print("File not found")
Output
File not found
Explanation: os.path.exists() helps prevent errors by checking whether a file is available before attempting to access it.
Additional Useful OS Path Functions
Besides the commonly used functions discussed above, the os.path module provides several other utilities that can be helpful when working with files and directories.
| Function | Description |
|---|---|
| os.path.join() | Joins multiple path components into a single path |
| os.path.exists() | Checks whether a path exists |
| os.path.abspath() | Returns the absolute path |
| os.path.splitext() | Splits a file name and its extension |
| os.path.getsize() | Returns the size of a file in bytes |
| os.path.getmtime() | Returns the last modified time of a file |
| os.path.realpath() | Returns the canonical path by resolving symbolic links |
| os.path.relpath() | Returns the relative path between two locations |