Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

OS Path module in Python

  • Last Updated : 29 Nov, 2020

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. 
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Python3




# 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. 
 



Python3




# dirname function
import 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. 
 

Python




# 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. 
 

Python




# 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. 
 

Python




# 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. 
 

Python




# normcase function in windows
import 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 . 
 

Python




# 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
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :