Open tischi opened 4 months ago
@k-dominik and chatGPT (see below) prefer pathlib
, thus I will use it now.
The use of pathlib versus os in Python has been evolving, with pathlib becoming increasingly popular
in recent years. Here are some points to consider:
1. Modern and Intuitive API: pathlib offers a more modern and intuitive API for working with file system
paths. It uses object-oriented principles, making it easier to understand and more readable. For example,
combining paths with / is more intuitive than using os.path.join .
2. Cross-Platform Consistency: pathlib ensures better cross-platform compatibility. While os can also
handle cross-platform paths, pathlib abstracts away more of the complexities, making your code cleaner and
less error-prone.
3. Functionality: pathlib provides more functionality out of the box, such as easy manipulation of paths,
file reading and writing, and more. It integrates well with other parts of the Python standard library.
4. Adoption and Trends: Since pathlib was introduced in Python 3.4 and became the recommended way to
handle file paths, its adoption has been growing. Many modern Python projects and codebases prefer pathlib
due to its simplicity and power.
5. Compatibility: For older codebases or environments that still use Python 2.x, os and os.path are
necessary as pathlib is not available. However, with Python 2.x no longer being maintained, this is
becoming less of a concern.
Here is a simple comparison:
Using os :
import os
tmp_folder = tempfile.gettempdir()
file_name = "nuclei.tif"
image_path = os.path.join(tmp_folder, file_name)
parent_folder = os.path.dirname(image_path)
filename_no_extension = os.path.splitext(os.path.basename(image_path))[0]
labels_path = os.path.join(tmp_folder, f"{filename_no_extension}_labels.tif")
Using pathlib :
from pathlib import Path
import tempfile
tmp_folder = Path(tempfile.gettempdir())
file_name = "nuclei.tif"
image_path = tmp_folder / file_name
parent_folder = image_path.parent
filename_no_extension = image_path.stem
labels_path = tmp_folder / f"{filename_no_extension}_labels.tif"
As you can see, the pathlib version is more concise and readable. This readability and ease of use are key
reasons why pathlib is becoming more common and preferred in modern Python development.
This activity currently uses
pathlib
.Is that good or too fancy? Should we rather use
os.path
?ping @manerotoni