OS Path Module In Python.

This Module provides some useful methods to manipulate files and directories. The path parameters are generally either in form of strings or bytes.

For different os systems, there are different versions of this module available in the standard library.

Following are some of the most useful methods of the path module.

1.  os.path.abspath(path)

returns normalized absolutized version of pathname path.

 # abspath function
import os
out = os.path.abspath("your path")
print(out)


2. os.path.basename(path)

returns the base name of the given path.

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

output:
foo

3. os.path.dirname(path)

used to return the directory name of the given path.

# dirname function
import os
out = os.path.dirname("dash/baz/foo")
print(out)
​
output:
dash/baz

4. os.path.isdir(path)

Specifies whether the path is an existing directory or not.

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

output:
true

5. os.path.isabs(path)

Used to check whether the specified path is absolute or not.

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

6. os.path.isfile(path)

Return true if the path is an existing directory.

# isfile function
import os
out = os.path.isfile("D:\backup\range")
print(out)
 
return :
true

7. os.path.normpath(path)

It normalizes the pathname by collapsing  redundant separators and up-level references.

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

output:
foo/bar

8. os.path.split(path)

Split the path into (head, tail ) pair where the tail is the last pathname component and the head is everything leading up to that.

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

output:
('foo', 'bar')

9. os.path.splitext(path)

Used to split the path and file extension

# splitext function in Unix
import os
out = os.path.splitext("c:/dir/backup.js")
print(out)

output:
('c:/dir/backup', '.js')

10. os.path.islink(path)

Returns the true if the path refers to the directory entry that is a symbolic link.

# islink function in Unix
import os
out = os.path.islink("c:/backup.js")
print(out)

output:
False

That’s it for now if you want to explore more links please visit the link below:

https://docs.python.org/3/library/os.path.html

Thanks for read.

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories