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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# basename function
import os
out = os.path.basename("/baz/foo")
print(out)

chevron_right


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.



filter_none

edit
close

play_arrow

link
brightness_4
code

# dirname function
import os
out = os.path.basename("/baz/foo")
print(out)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# isabs function 
import os
out = os.path.isabs("/baz/foo")
print(out)

chevron_right


Output:

True

4. os.path.isdir(path) : This function specifies whether the path is existing directory or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

# isdir function 
import os
out = os.path.isdir("C:\\Users")
print(out)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# normcase function in windows
import os
out = os.path.normcase("/BAz")
print(out)

chevron_right


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 .

filter_none

edit
close

play_arrow

link
brightness_4
code

# normpath function in Unix
import os
out = os.path.normpath("foo/./bar")
print(out)

chevron_right


Output:

'foo/bar'

There are many more functions , you can refer that in python.

References :
Python Documentation



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.