OS Path module in Python
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 function import 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 function import os out = os.path.basename("/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.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 windows import os out = os.path.normcase("/BAz") print(out) |
Output:
'\\baz'
6. 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 Unix import 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
Recommended Posts:
- pwd module in Python
- grp module in Python
- Fraction module in Python
- struct module in Python
- C Extension Module using Python
- Import module in Python
- Pylatex module in python
- Python | Calendar Module
- OS Module in Python with Examples
- Python Urllib Module
- spwd module in Python
- Pygorithm module in Python
- C API from Extension Module in Python | Set 1
- Pathlib module in Python
- C API from Extension Module in Python | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




