Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23137

Everyday Superpowers: Finding the root of a project with pathlib

$
0
0

Every now and then I'm writing code deep in some Python project, and I realize that it would be nice to generate a file at the root of a project.

The following is the way I'm currently finding the root folder with pathlib:

if __name__ == '__main__':
    from pathlib import Path
    project_root = next(
        p for p in Path(__file__).patents)
        if (p / '.git').exists()
    )
    project_root.joinpath('output.text').write_text(...)

Read more...

Viewing all articles
Browse latest Browse all 23137

Trending Articles