Created
April 4, 2018 00:45
-
-
Save nickovs/cec1e1624b47224e0492d17e30a2addb to your computer and use it in GitHub Desktop.
Easier handling of file paths in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A Python (3) class for more compact path handling. | |
# | |
# If you find yourself calling os.path.join() with the same first parameter | |
# over and over again it can get rather tedious. Instead you can now go: | |
# | |
# path = PathPrefix("/some/path") | |
# file1 = path / "file1" | |
# sub_file = path / "subdir" / "file2" | |
class PathPrefix(str): | |
def __truediv__(self, other): | |
return PathPrefix(os.path.join(self, other)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If for some reason you want to do this in Python 2.x then replace
__truediv__
with__div__
.