Path objects also override the / operator to join paths
This is both cool and gross... gives me C++ vibes (operator overloading abuse).
Welcome to the Python community on the programming.dev Lemmy instance!
Past
November 2023
October 2023
July 2023
August 2023
September 2023
Path objects also override the / operator to join paths
This is both cool and gross... gives me C++ vibes (operator overloading abuse).
They could have chosen a better operator. But the functionality is fantastic. Makes working with paths so much easier. And you can even use slashes on windows paths.
It makes the code so much more clean and readable since you're dropping multiple levels of brackets , for example
os.path.join( a, b, os.path.dirname(c))
Becomes
a / b / c.parent
I really like it
Nice, mypath.open() is a more semantic alternative to open(my path)
Don't forget about the helper functions mypath.read_text()
and mypath.write_text(content)
that's already in the cheatsheet