This module contains some useful functions on pathnames. The path parameters are either strings or bytes . These functions here are used for different purposes such as for merging, normalizing and retrieving path names in python . All of these functions accept either only bytes or only string objects as their parameters. The result is an object of the same type, if a path or file name is returned. As there are different versions of operating system so there are several versions of this module in the standard library.
Following are some functions of OS Path module.
1. os.path.basename(path) : It is used to return the basename of the file . This function basically return the file name from the path given.
# basename functionimport os
out = os.path.basename("/baz/foo")
print(out)
|
Output:
'foo'
2. os.path.dirname(path) : It is used to return the directory name from the path given. This function returns the name from the path except the path name.
# dirname functionimport os
out = os.path.dirname("/baz/foo")
print(out)
|
Output:
'/baz'
3. os.path.isabs(path) : It specifies whether the path is absolute or not. In Unix system absolute path means path begins with the slash(‘/’) and in Windows that it begins with a (back)slash after chopping off a potential drive letter.
# isabs function import os
out = os.path.isabs("/baz/foo")
print(out)
|
Output:
True
4. os.path.isdir(path) : This function specifies whether the path is existing directory or not.
# isdir function import os
out = os.path.isdir("C:\\Users")
print(out)
|
Output:
True
5. os.path.isfile(path) : This function specifies whether the path is existing file or not.
# isfile function import os
out = os.path.isfile("C:\\Users\foo.csv")
print(out)
|
Output:
True
6. os.path.normcase(path) : This function normalizes the case of the pathname specified. In Unix and Mac OS X system it returns the pathname as it is . But in Windows it converts the path to lowercase and forward slashes to backslashes.
# normcase function in windowsimport os
out = os.path.normcase("/BAz")
print(out)
|
Output:
'\\baz'
7. os.path.normpath(path) : This function normalizes the path names by collapsing redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B. On Windows, it converts forward slashes to backward slashes .
# normpath function in Uniximport os
out = os.path.normpath("foo/./bar")
print(out)
|
Output:
'foo/bar'
There are many more functions , you can refer that in python.
References :
Python Documentation

