os.path.diff(path1, path2)
Just wondering if a function such as this has ever been considered? I find that I quite often want a function that will give me a relative path from path A to path B. I have created such a function, but it would be nice if it was in the standard library. This function would take two paths: A and B and give the relation between them. Here are a few of examples. os.path.diff("/A/C/D/", "/A/D/F/") ==> "../../D/F" os.path.diff("/A/", "/A/B/C/") ==> "B/C" os.path.diff("/A/B/C/", "/A/") ==> "../.." I suppose it would also be nice if you could use path + file. For example: os.path.diff("/A/C/D/xyz.html", "/A/D/F/zlf.html") ==> "../../D/F/zlf.html" I am not subscribed to the list so if anyone thinks this is useful please CC my email address. I also want to say thank you to everyone who has made Python what it is today. Nathan Bullock Visit my website at http://www.nathanbullock.org __________________________________________________________ Find your next car at http://autos.yahoo.ca
[Nathan Bullock wrote]
Just wondering if a function such as this has ever been considered? I find that I quite often want a function that will give me a relative path from path A to path B. I have created such a function, but it would be nice if it was in the standard library.
This function would take two paths: A and B and give the relation between them. Here are a few of examples.
os.path.diff("/A/C/D/", "/A/D/F/") ==> "../../D/F"
os.path.diff("/A/", "/A/B/C/") ==> "B/C"
os.path.diff("/A/B/C/", "/A/") ==> "../.."
Look around for functions/recipes called "relpath". E.g.: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302594 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/208993 http://www.jorendorff.com/articles/python/path/ Trent -- Trent Mick TrentM@ActiveState.com
On 9/12/05, Nathan Bullock <nathan_kent_bullock@yahoo.ca> wrote:
Just wondering if a function such as this has ever been considered? I find that I quite often want a function that will give me a relative path from path A to path B. I have created such a function, but it would be nice if it was in the standard library.
I also often have to plop a snippet of Python in my code to do the same and always wonder why there isn't anything like that in the standard library. +1 for adding this to os.path, it would be very handy.
[Greg Ewing wrote]
Nathan Bullock wrote:
I find that I quite often want a function that will give me a relative path from path A to path B. I have created such a function, but it would be nice if it was in the standard library.
+1 from me. It's a fairly common thing to want to do.
If this *does* get added (I'm +0) then let's call it "relpath" or "relpathto" as in the various implementations out there: http://www.jorendorff.com/articles/python/path/ http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302594 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/208993 Trent -- Trent Mick TrentM@ActiveState.com
Trent Mick wrote:
If this *does* get added (I'm +0) then let's call it "relpath" or "relpathto" as in the various implementations out there:
+1 on that, too. Preferably just "relpath". -- Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg.ewing@canterbury.ac.nz +--------------------------------------+
On Fri, 16 Sep 2005, Greg Ewing wrote:
Trent Mick wrote:
If this *does* get added (I'm +0) then let's call it "relpath" or "relpathto" as in the various implementations out there:
+1 on that, too. Preferably just "relpath". [...]
+1 on adding this function, and on "relpath" as the name. I wanted this function just a few weeks ago. John
Hi,
This function would take two paths: A and B and give the relation between them. Here are a few of examples.
os.path.diff("/A/C/D/", "/A/D/F/") ==> "../../D/F"
os.path.diff("/A/", "/A/B/C/") ==> "B/C"
os.path.diff("/A/B/C/", "/A/") ==> "../.."
I'm not sure whether something like this is generally non-trivial. Suppose you have the following directory structure: /home -> usr/home /usr /usr/home What does os.path.diff("/home/", "/usr/") yield? "../usr/", I would presume? But that's obviously wrong:
import os os.stat("/home/../usr") Traceback (most recent call last): File "<stdin>", line 1, in ? OSError: [Errno 2] No such file or directory: '/home/../usr'
I've not thought about this long enough to say if there's a trivial solution to this, though :) Bye, Matthias A. Benkard ________________________________________________________________________ Matthias Andreas Benkard, Anarchokommunist und Pythonprogrammierer Persönliche Website: http://www.mulk.de.vu/ Persönlicher Weblog: http://www.kompottkin.de.vu/ Flames bitte nach /dev/null schicken.
Hi,
/home -> usr/home
Sorry, I forgot to mention what I meant by this: /home is a symlink pointing to usr/home (that is, /usr/home). Bye, Matthias ________________________________________________________________________ Matthias Andreas Benkard, Anarchokommunist und Pythonprogrammierer Persönliche Website: http://www.mulk.de.vu/ Persönlicher Weblog: http://www.kompottkin.de.vu/ Flames bitte nach /dev/null schicken.
Matthias Andreas Benkard wrote:
/home -> usr/home /usr /usr/home
What does os.path.diff("/home/", "/usr/") yield? "../usr/", I would presume? But that's obviously wrong:
IMO, the relpath method should just work textually on the pathnames. It's up to the user to ensure it makes sense to do so, e.g. by resolving symlinks beforehand if necessary. The alternative would be for relpath to do this itself, but that would make it very unusual compared to the other path-munging functions, none of which touch the file system. -- Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg.ewing@canterbury.ac.nz +--------------------------------------+
Hi,
IMO, the relpath method should just work textually on the pathnames. It's up to the user to ensure it makes sense to do so, e.g. by resolving symlinks beforehand if necessary.
I guess so. Don't forget to mention this quirk in the docs, though :)
The alternative would be for relpath to do this itself, but that would make it very unusual compared to the other path-munging functions, none of which touch the file system.
Yes, and this behaviour would make it unusable for virtual file systems within the application, too. Path names needn't refer to the local file system, right? Then again, the `os' module _is_ platform-specific, as is `os.path', so one shouldn't use it for those things either way. In fact, relpath _cannot_ dereference symlinks on its own, as it has no way of knowing the absolute pathname if only relative pathnames are given as arguments (for example, relpath("a/b/c", "../d")). But this begs the question: What is relpath good for, anyway? And what are the hidden costs? One-time scripts might benefit from it, but having this function in the standard library could make application developers use it without considering the consequences, resulting in strange bugs that occur on one machine, but not on another. The more I think about it, the more I'm afraid that this might lead to severe security vulnerabilities if misused in tools run by the system administrator. Better place a big, fat warning in the docs, then. Bye, Matthias ________________________________________________________________________ Matthias Andreas Benkard, Anarchokommunist und Pythonprogrammierer Persönliche Website: http://www.mulk.de.vu/ Persönlicher Weblog: http://www.kompottkin.de.vu/ Flames bitte nach /dev/null schicken.
Matthias Andreas Benkard wrote:
But this begs the question: What is relpath good for, anyway?
[Assuming you mean "invites", "prompts", etc. the question...:-)] A couple of use cases I've encountered: * Creating symbolic links. I'm traversing a directory hierarchy, and building a parallel hierarchy somewhere else that contains symbolic links into the original one. I want the links to be relative, so that e.g. I can move my home directory without all my links breaking. * In a recent project, I had occasion to store pathnames of picture files in a relational database. I wanted to store the pathnames relative to a user-chosen "Pictures" directory, so that it could be moved without having to update all the pathnames in the database. Both of these happen to involve pathnames that exist on the currrently available file system, but I can easily imagine cases where that would not be so. E.g. I might be generating pathames to go into a tar file that will be unpacked in a different place or on another system. -- Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg.ewing@canterbury.ac.nz +--------------------------------------+
But this begs the question: What is relpath good for, anyway?
A couple of use cases I've encountered:
Another one: - Build tools that work with paths alot can really improve their log readability if relative paths can be used to keep paths short rather than the common fallback of just making everything an absolute path. Trent -- Trent Mick TrentM@ActiveState.com
On Wed, 21 Sep 2005 02:55:56 +0200, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Both of these happen to involve pathnames that exist on the currrently available file system, but I can easily imagine cases where that would not be so. E.g. I might be generating pathames to go into a tar file that will be unpacked in a different place or on another system.
imho, it would be a good thing for a future 'file system handling module' to build more of an abstracted tree-like graph that may or may not be mappable (and may or may not be used in a particular case to actually map) to existing objects on a particular system. for example, i find it a bit in my way a lot of times that all the locators i have os.path handle for me are written according to the os, not in a unified, abstracted way. given that there are a number of applications (xml documents, file system handling, urls, archives...) that have very similar requirements, maybe there is a useful abstraction that covers these and other cases. some nitty-gritty details could be captured by suitable specializations of the general case. _w
On 9/20/05, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Matthias Andreas Benkard wrote:
* In a recent project, I had occasion to store pathnames of picture files in a relational database. I wanted to store the pathnames relative to a user-chosen "Pictures" directory, so that it could be moved without having to update all the pathnames in the database.
Curator does the same, for the same reasons. http://furius.ca/curator/ Also, you can burn the static HTML files to CDROM, and the links still all work. That was the main motivator for this. cheers,
participants (8)
-
Greg Ewing -
John J Lee -
Martin Blais -
Matthias Andreas Benkard -
Matthias Andreas Benkard -
Nathan Bullock -
Trent Mick -
Wolfgang Lipp