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