Saturday, November 16, 2024
Google search engine
HomeLanguagesOS Path module in Python

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. 
 

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
 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments