Skip to content

Instantly share code, notes, and snippets.

@aphlysia
Created January 7, 2013 13:53
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 aphlysia/4475152 to your computer and use it in GitHub Desktop.
Save aphlysia/4475152 to your computer and use it in GitHub Desktop.
import os.path
class Path:
def __init__(self, path = os.path.curdir):
assert isinstance(path, str) or isinstance(path, self.__class__)
if isinstance(path, str):
self.path = path
else:
self.path = path.path
def __str__(self):
return self.path
def __add__(self, other):
assert isinstance(other, str) or isinstance(other, self.__class__)
if isinstance(other, str):
return self.__class__(os.path.join(self.path, other))
else:
return self.__class__(os.path.join(self.path, other.path))
def __iadd__(self, other):
assert isinstance(other, str) or isinstance(other, self.__class__)
if isinstance(other, str):
self.path = os.path.join(self.path, other)
else:
self.path = os.path.join(self.path, other.path)
return self
@property
def abspath(self):
return self.__class__(os.path.abspath(self.path))
@property
def basename(self):
return self.__class__(os.path.basename(self.path))
@property
def dirname(self):
return self.__class__(os.path.dirname(self.path))
@property
def expanduser(self):
return self.__class__(os.path.expanduser(self.path))
@property
def normpath(self):
return self.__class__(os.path.normpath(self.path))
@property
def realpath(self):
return self.__class__(os.path.realpath(self.path))
@property
def relpath(self):
return self.__class__( os.path.relpath(self.path))
def join(self, *p):
return self.__class__( os.path.join(self.path, *p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment