Home » Pathlib module in Python

Pathlib module in Python

by Online Tutorials Library

Pathlib module in Python

Interacting with the file system and working with files is crucial for various reasons. The simplest situations may merely entail reading or writing files, but more sophisticated actions are occasionally required. Perhaps you need to show all files in a directory of a specific kind, locate a file’s parent directory, or generate a new file name that doesn’t exist.

It’s impossible to develop a Python script that doesn’t use the file system somehow. It might be as easy as reading a data file into a pandas DataFrame or as difficult as processing hundreds of files in a highly nested directory structure. For these purposes, Python’s standard library includes numerous useful methods, notably the pathlib module.

The pathlib module initially appeared in Python 3.4, and it has since been improved in each succeeding version. Pathlib is an object-oriented filesystem interface that provides a more natural, platform-agnostic, and pythonic way to interact with the filesystem. I recently completed a short project in which I opted to sort and handle thousands of files in a nested directory structure using pathlib and pandas. That project inspired this article. Once it all came together, I was blown away by pathlib’s capabilities, and I’ll be using it in future projects without hesitation. All versions of Python >= 3.4 contain the pathlib library. To obtain all of the newest upgrades, I recommend using the most recent version of Python. I’ll be using Python 3.6 in this post.

One of the advantages of the pathlib module is that it is easier to create routes without using os.joindir. When I start tiny projects, for example, I use os.getcwd() to create in and out directories as subdirectories beneath the current working directory. I keep the working input and output files in those locations. This is how that code might look:

Syntax:

Traditionally, Python has used ordinary text strings to represent file paths. This has proved adequate, if a little laborious, with help from the os.path standard library (as the second example in the introduction shows). However, because paths are not strings, key functionality is dispersed across the standard library, including libraries such as os, glob, and shutil. To move all text files to an archive directory, the following example requires three import statements:

Syntax:

It is conceivable, but usually, a terrible idea, to utilise standard string methods with pathways represented by strings. Instead of joining two paths with + as you would with conventional strings, you should use os.path.join(), which joins paths using the operating system’s correct path separator. Remember that on Windows, the separator is /, whereas, on Mac and Linux, it is /. This distinction can result in difficult-to-detect problems like our first example in the introduction, which only works for Windows paths.

To address these issues, the pathlib module was added in Python 3.4 (PEP 428). It collects all of the required functionality in one place and makes it accessible via methods and properties on a simple Path object. Other packages used strings for file paths at first, but since Python 3.6, the pathlib module has been supported throughout the standard library, thanks in part to creating a file system path protocol. A backport for Python 2 is also available if one is stuck with legacy Python.

Code:

Output:

[email protected]:/# python3 /home/nirnay/code.py   Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  1  >Enter the absolute path of the directory whose sub-directories you want to list::  /directory1  /directory1/directory4  /directory1/directory3  /directory1/directory2  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  2  >Enter the absolute path of the directory whose files you want to list::  /directory1  /directory1/java_code.java  /directory1/c++_code.c++  /directory1/python_code.py  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  3  >Enter the absolute path of the file which you want to check is present::  /directory1/python_code.py  The file is present.  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  4  The current working directory is: /  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  5  >Enter the absolute path to the new directory to which you want to change::  /directory1  The Current working directory changed successfully from / to /directory1  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  4  The current working directory is: /directory1  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  1  >Enter the absolute path of the directory whose sub-directories you want to list::  /directory1  /directory1/directory4  /directory1/directory3  /directory1/directory2  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  5  >Enter the absolute path to the new directory to which you want to change::  /directory1/directory2  The Current working directory changed successfully from /directory1 to /directory1/directory2  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  4  The current working directory is: /directory1/directory2  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  2  >Enter the absolute path of the directory whose files you want to list::  /directory1/directory2  /directory1/directory2/files6  /directory1/directory2/files1  /directory1/directory2/files3  /directory1/directory2/files4  /directory1/directory2/files2  /directory1/directory2/files5  /directory1/directory2/files8  /directory1/directory2/files10  /directory1/directory2/files7  /directory1/directory2/files9  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  5  >Enter the absolute path to the new directory to which you want to change::  /directory1/directory4  The Current working directory changed successfully from /directory1/directory2 to /directory1/directory4  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  5  >Enter the absolute path to the new directory to which you want to change::  /directory1/directory3  The Current working directory changed successfully from /directory1/directory4 to /directory1/directory3  Do you want to continue or exit the code execution?[y/n]  y  Please choose one of the appropriate options::  1. To print all the sub-directories of a folder/dir.  2. To print all the files present inside a folder/directory.  3. Check the existence of a specified file  4. To check the current directory of the code execution.  5. To modify the current directory of the code execution. (using the chdir() function)  6. To exit from the code execution.  4  The current working directory is: /directory1/directory3  Do you want to continue or exit the code execution?[y/n]  n  [email protected]:/# tree directory1    directory1/  ├── c++_code.c++  ├── directory2  │   ├── files1  │   ├── files10  │   ├── files2  │   ├── files3  │   ├── files4  │   ├── files5  │   ├── files6  │   ├── files7  │   ├── files8  │   └── files9  ├── directory3  │   ├── files11  │   ├── files12  │   ├── files13  │   ├── files14  │   ├── files15  │   ├── files16  │   ├── files17  │   ├── files18  │   ├── files19  │   └── files20  ├── directory4  │   ├── files21  │   ├── files22  │   ├── files23  │   ├── files24  │   ├── files25  │   ├── files26  │   ├── files27  │   ├── files28  │   ├── files29  │   └── files30  ├── java_code.java  └── python_code.py  

Explanation:

Before looking into the output of the above-written code, the directory structure shows the parent directory is named directory 1 and has three children directories named directory 2, directory 3 and directory 4, respectively. There are 10 files present inside each child’s directory, and in the parent directory, apart from these three children’s directories, three files are also present.

Now let us look at the output of the above-written code, on the execution of the above-written program, the user is prompted with 6 options first one is to list all the subdirectories that are present inside a parent directories second option is to list all the files that are present in a particular directory option 3 to check whether a particular file is existing at a particular location or not option 4 is to print the current working directory of the code execution and option 5 is to change the current working directory of the code execution to a new directory that is specified as a parameter to this functionality method. and in the last 6 options is to execute the code execution.

Conclusion

So, in this article, we have understood the pathlib module in python and have seen various use case scenarios where we can use the different functionalities offered by the pathlib module.


You may also like