Skip to content

Instantly share code, notes, and snippets.

@nickovs
Created April 4, 2018 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickovs/cec1e1624b47224e0492d17e30a2addb to your computer and use it in GitHub Desktop.
Save nickovs/cec1e1624b47224e0492d17e30a2addb to your computer and use it in GitHub Desktop.
Easier handling of file paths in Python
# 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))
@nickovs
Copy link
Author

nickovs commented Apr 4, 2018

If for some reason you want to do this in Python 2.x then replace __truediv__ with __div__.

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