Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with python file paths #704

Open
tischi opened this issue Jul 8, 2024 · 1 comment
Open

How to deal with python file paths #704

tischi opened this issue Jul 8, 2024 · 1 comment

Comments

@tischi
Copy link
Collaborator

tischi commented Jul 8, 2024

This activity currently uses pathlib.

Is that good or too fancy? Should we rather use os.path?

ping @manerotoni

@tischi
Copy link
Collaborator Author

tischi commented Jul 8, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant