Allen Fowler wrote: >>> FWIW: >>> >>> When using relative paths I got extra ../../ terms, so I changed >> join_relative() to: >>> def join_relative(base, path): >>> return os.path.normpath(os.path.join(script_dir(base), path)) >>> >>> >>> Seems to work... >> >> Yeah, good catch ... looks great, and thanks for sharing your mod. >> > > > Glad I could return the favor. :) > > Since then, I took it a bit further for use in my code... I am not finished > with it yet, but am curios what you think. (I am not sure named things right > or structured it logically....) > > > ################### > > import os > class HomePath(object): > """For finding paths based on a home/install directory. > > """ > > def __init__(self, home_dir = None, home_file = None): > """Must be called with either a path to a directory or, as a > shortcut, a file in that directory. > > """ > > if home_file != None: > # Set home based on a path to a file in its directory > self.home = os.path.normpath(self.fix_path(home_file)) > > elif home_dir != None: > # Set home based on its path > self.home = os.path.normpath(self.get_dir(home_dir)) > > else: > raise Exception("Must call with either a path to a directory > or, as a shortcut, a file in that directory.") > > def abs(self, rel_from_home): > """Return an absolute path when passed a path relative to home. > > """ > > return self.join_relative(self.home, rel_from_home) > > def fix_path(self, base): > return os.path.realpath(os.path.abspath(base)) > > def get_dir(self, base): > return os.path.dirname(self.fix_path(base)) > > def join_relative(self, base, path): > return os.path.normpath(self.fix_path(os.path.join(self.home, > path))) > #############################
I'm not sure I understand your end goal, given the args to __init__. I'd guess, perhaps incorrectly, that you may be trying to shoehorn conflicting purposes. If your only aim is to calculate an absolute path to a resource's parent, or home, folder then your class can be simplified a lot, IMHO. Specifically, I don't see a reason to differentiate between home_dir and home_file. Please feel free to clarify if I'm missing the point. import os class HomePath(object): def __init__(self, path): # fix_path and get_dir on base path self.home = os.path.dirname( os.path.realpath( os.path.abspath(path) ) ) def join(self, relative_path): return os.path.normpath(os.path.join(self.home, relative_path)) Also, have a look at http://docs.python.org/library/os.path.html#os.path.abspath ... the main point is that your initial path element must have enough information in it to construct an abspath, otherwise os.getcwd() can pollute the calculation. I believe if you use __file__ you're good, and sys.argv[0], while potentially relative in nature should always be relative to os.getcwd() AFAICT, so you're good there too. Otherwise avoid constructing a HomePath object with a relative start path. HTH, Marty _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor