3.9. pathlib#

pathlib is a Python library that makes it easy to handle file in Python.

3.9.1. Create a New Directory and File#

You can use either os or pathlib to create new directories and files in Python. pathlib is my preferred choice because it is simpler to use and easier to understand.

import os

# Create a new directory
if not os.path.exists("new"):
    os.makedirs("new")

# Create a new file inside new directory
file = os.path.join("new", "new_file.txt")

# Write text to the file
with open(file, "w") as f:
    f.write("Hello World!")

# Read the file
with open(file, "r") as f:
    print(f.read())
Hello World!
from pathlib import Path

# Create a new directory
folder = Path("new")
folder.mkdir(exist_ok=True)

# Create new file inside new directory
file = folder / "new_file.txt"

# Write text
file.write_text("Hello World!")

# Read text
file.read_text()
12

The following is the structure of the new folder:

.
└── new/
    └── new_file.txt

3.9.2. Get Access to Files from Home Directory#

If you want to get the path to folders/files from the home directory, use Path.home()

from pathlib import Path

path = Path.home()

docs = path / 'Documents'
pictures = path / 'Pictures'

print(docs)
print(pictures)
/Users/khuyen/Documents
/Users/khuyen/Pictures

Now you can use pathlib’s methods to manipulate the folders/files in the obtained path.

# Create a new file inside Documents
file = docs / 'new_file.txt'
file.touch()
!tree /Users/khuyen/Documents | grep new_file.txt 
├── new_file.txt

3.9.3. Get the Parent of the Current Path with pathlib#

If you want to get the parent path or the grandparent path easily, use pathlib’s .parent.

from pathlib import Path

path = Path.cwd()

print(f'Current path: {path}')
print(f'Parent of the current path: {path.parent}')
print(f'Grandparent of the current path: {path.parent.parent}')
Current path: /Users/khuyen/book/Efficient_Python_tricks_and_tools_for_data_scientists/Chapter2
Parent of the current path: /Users/khuyen/book/Efficient_Python_tricks_and_tools_for_data_scientists
Grandparent of the current path: /Users/khuyen/book

3.9.4. Get the Path Relative to Another Path#

If you want to get the path relative to another path, use pathlib.Path.relative_to.

from pathlib import Path

nlp = Path('/Users/khuyen/Data-science/nlp/')
root = '/Users/khuyen/'
nlp.relative_to(root)
PosixPath('Data-science/nlp')

3.9.5. Check if Two File Paths Are the Same#

If you want to check whether two file paths are the same, use Path.samefile().

from pathlib import Path 

home = Path.home()
absolute = home / "book/book/Chapter2/pathlib.ipynb"
relative = Path("pathlib.ipynb")

absolute.samefile(relative)
True