Python Pathlib

Refer: https://stackabuse.com/introduction-to-the-python-pathlib-module/


import pathlib
current_dir = pathlib.Path.cwd()  
home_dir = pathlib.Path.home()  
print(current_dir)  
print(home_dir)

Though the code works, it looks clunky and is not readable nor easy to maintain. Imagine how this code would look if we wanted to create a new file inside multiple nested directories.

The same code can be re-written using Pathlib module, as follows:


import Path  
outpath = Path.cwd() / 'output' / 'output.xlsx'  

Leave a Reply