Pathlib module in Python

Pathlib module in Python provides various classes representing file system paths with semantics appropriate for different operating systems. This module comes under Python’s standard utility modules.

Path classes in Pathlib module are divided into pure paths and concrete paths. Pure paths provides only computational operations but does not provides I/O operations, while concrete paths inherit from pure paths provides computational as well as I/O operations.

Pathlib-module-classes-relation-diagram



Pure Paths –
As stated above, Pure paths provide purely computational operations. Objects of pure path classes provide various methods for path handling operations. Pure path object operates without actually accessing the file system.
Pure Path is useful when we just want to manipulate a path and without actually accessing the operating system. We can manipulate a Windows file system path on a Unix machine or vice-versa easily by instantiating one of the pure classes.

class pathlib.PurePath(*pathsegments) –
This is a generic class which represents the system path’s flavour. Upon instantiating this class will create either pathlib.PurePosixPath or pathlib.PureWindowsPath

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import PurePath class
# from pathlib module
from pathlib import PurePath
  
# Instantiate the PurePath class
obj = PurePath('foo/baar')
  
# print the instance of PurePath class
print(obj)

chevron_right


Output:

PurePosixPath('foo/baar')

 
class pathlib.PurePosixPath(*pathsegments) –
This is a subclass of PurePath class. It represents non-Windows file system paths.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import PurePosixPath class
# from pathlib module
from pathlib import PurePosixPath
  
# Instantiate the PurePosixPath class
obj = PurePosixPath('foo / baar')
  
# print the instance of PurePosixPath class
print(obj)

chevron_right


Output:

PurePosixPath('foo/baar')

 
class pathlib.PureWindowsPath(*pathsegments) –
This is also a subclass of patlib.PurePath class. It represents Windows file system paths.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import PureWindowsPath class
# from pathlib module
from pathlib import PureWindowsPath
  
# Instantiate the PureWindowsPath class
obj = PureWindowsPath('foo / baar')
  
# print the instance of PureWindowsPath class
print(obj)

chevron_right


Output:

PureWindowsPath('foo/baar')

 
Below are few methods provided by Pure Path classes:

PurePath.is_absolute() method –
This method is used to check whether the path is absolute or not. This method returns True if the path is absolute otherwise returns False.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain PurePath.is_absolute() method
  
# Import PurePath class from pathlib module
from pathlib import PurePath
  
# Path
path = '/usr / local / bin'
  
# Instantiate the PurePath class
obj = PurePath(path)
  
# Check whether the given path is 
# absolute or not
isAbs = obj.is_absolute()
  
print(isAbs)

chevron_right


Output:


True

 
PurePath.name property –

This Pure path property returns the final path component after excluding drive and root component, if any.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain PurePath.name property
  
# Import PurePath class from pathlib module
from pathlib import PurePath
  
# Path
path = '/Desktop / file.txt'
  
# Instantiate the PurePath class
obj = PurePath(path)
  
# Get the final path component
comp = obj.name
  
print(comp)

chevron_right


Output:

file.txt

Concrete Paths:

Concrete paths are sub-classes of pure path classes. As we know pure path class provides only computational operations but it does also provides various methods to perform system call on path objects.

We can instantiate a concrete path in following three ways:

class pathlib.Path(*pathsegments) –
This is a subclass of pathlib.PurePath class. It represents concrete paths of the system’s path flavour. Upon instantiating, this class will create either pathlib.PosixPath or pathlib.WindowsPath.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import the Path class
from pathlib import Path
  
# Instantiate the Path class
obj = Path('/usr/local/bin)
  
print(obj)

chevron_right


Output:

PosixPath('/usr/local/bin')

 
class pathlib.PosixPath(*pathsegments) –
This class is a subclass of pathlib.Path and pathlib.PurePosixPath class. This class represents the concrete non-Windows file system paths.

Note: You can not instantiate pathlib.Posixpath class on Windows operating system.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import PosixPath class
# from pathlib module
from pathlib import PosixPath
  
# Instantiate the PosixPath class
obj = PosixPath('/usr/local/bin')
  
# Print the instance of PosixPath class
print(obj)

chevron_right


Output:


PosixPath('/usr/local/bin')

 
class pathlib.WindowsPath(*pathsegments) –

This class is a subclass of pathlib.Path and pathlib.PureWindowsPath. This class represents the concrete Windows file system paths.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import WindowsPath class
# from pathlib module
from pathlib import WindowsPath
  
# Instantiate the WindowsPath class
obj = WindowsPath('c:/Program Files/')
  
# print the instance of WindowsPath class
print(obj)

chevron_right


Output:

WindowsPath('c:/Program Files/')

 

Below are few methods provided by Path class:
Path.cwd() method: This method returns a new path object which represents the current working directory.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import Path class
from pathlib import Path
  
# Get the current working directory name
cur_dir = Path.cwd()
  
print(cur_dir)

chevron_right


Output:

/home/ihritik

 
Path.exists() method: This method is used to check whether the given path points to an existing file or directory or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import Path class
from pathlib import Path
  
# Path
path = '/home/ihritik/Desktop'
  
# Instantiate the Path class
obj = Path(path)
  
# Check if path points to 
# an existing file or directory 
print(obj.exists())

chevron_right


Output:

True

 
Path.is_dir() method: This method is used to check whether the given path is a directory or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import Path class
from pathlib import Path
  
# Path
path = '/home/ihritik/Desktop'
  
# Instantiate the Path class
obj = Path(path)
  
# Check if path refers to 
# directory or not 
print(obj.is_dir())

chevron_right


Output:

True

Reference – https://docs.python.org/3/library/pathlib.html



My Personal Notes arrow_drop_up

self motivated and passionate programmer

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.