Python pathlib Module - 14 Practical Examples

Python pathlib module provides an object-oriented approach to work with files and directories. The pathlib module has classes to work with Unix as well as Windows environments. The best part is that we don’t have to worry about the underlying operating system, the pathlib module takes care of using the appropriate class based on the operating system.

Python pathlib Path Class

Path is the most important class in the pathlib module. This is the entry point of all the functions provided by pathlib module. It takes care of instantiating the concrete path implementation based on the operating system and make the code platform-independent.

Python pathlib Module


Python pathlib Module

Let’s look into some examples of using pathlib module.

1. List Subdirectories and Files inside a Directory

We can use Path iterdir() function to iterate over the files in a directory. Then we can use is_dir() function to differentiate between a file and a directory.


If you run the same program in Windows, you will get instances of WindowsPath.

2. Listing specific type of files

We can use Path glob() function to iterate over a list of files matching the given pattern. Let’s use this function to print all the python scripts inside a directory.


Python pathlib Module


Python Pathlib List Files

3. Resolving Symbolic Links to Canonical Path

We can use resolve() function to convert the symbolic links to their canonical path.


4. Check if a File or Directory Exists

The Path exists() function returns True if the path exists, otherwise it returns False.

5. Opening and Reading File Contents

We can use Path open() function to open the file. It returns a file object like the built-in open() function.


6. Getting Information of the File

The Path object stat() function make the stat() system call and return the results. The output is the same as the os module stat() function.


7. Getting the File or Directory Name

We can use “name” property to get the file name from the path object.


8. Creating and Deleting a Directory

We can use mkdir() function to create a directory. We can use rmdir() to delete an empty directory. If there are files, then we have to delete them first and then delete the directory.

9. Change File Mode

The chmod() function behaves same as os.chmod() function to change the file permissions.

10. Getting File Group and Owner Name

11. Expand ~ to Canonical Path

12. CWD and Home Path


13. Joining Two Paths

14. Creating an empty file

Just like Unix touch command, Path has touch() function to create an empty file. You should have the permissions to create the file. Otherwise, the file won’t be created and there will be no error thrown.


Python Pathlib Create New File


Python pathlib module is very useful in working with files and directories in an object-oriented way. The loosely coupled and platform-independent code makes it more attractive to use.

Reference: Official Docs – pathlib module

By admin

Leave a Reply