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#
If you want to create new directories and files, you can either use os or pathlib. However, pathlib’s syntax is more straightforward and easier to understand than os’s syntax.
import os
path = 'new'
file = 'new_file.txt'
# Create a new directory
if not os.path.exists(path):
os.makedirs(path)
# Create new file inside new directory
with open(os.path.join(path, file), 'wb'):
pass
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'
file.touch()
!tree new
new
└── new_file.txt
0 directories, 1 file
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