![]() |
VOOZH | about |
os.path.basename() method in Python is used to extract the final part of a file path. This final part can be a file name or the last folder name. It is commonly used when working with file paths to get only the name without the full path.
file.txt
Explanation:
os.path.basename(path)
Example 1: This example extracts the file name from a relative path. This is useful when working with local project files.
data.csv
Explanation:
Example 2: This example extracts the last folder name from a directory path.
Documents
Explanation:
Example 3: This example shows what happens when the path ends with a trailing slash. It helps understand how basename() behaves with folder paths ending in /.
Explanation: