Python Forum
function wanted: resolve relative path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function wanted: resolve relative path
#1
i would like a function, that given paths to 2 directories, in either absolute or relative form, determines the path to reference directory 1 from directory 2 (or visa-versa). this could mean a lot of "../" in the result. the intended use is making relative symlinks. if the 2 directories are the same directory, the result obviously should be ".".

a plus would be support for the target to be any file system object type, not just a directory. another plus is not requiring either to actually exist, if the paths can still be resolved to absolute or to each other somehow (they might be created in the future ... such as generating a script that creates the objects and a symlink between them.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Here is a first attempt with pathlib
from pathlib import Path

d0 = Path('/home/me/project/spam/doc')
d1 = Path('/home/me/lib/spam')
d2 = d1 / 'foo' / 'bar'

class Out:
    def __lshift__(self, *args):
        for a in args:
            print(a, end=' ')
        return self
cout = Out()
endl = '\n'

def compute_link(path, base):
    b = base
    res = Path()
    while True:
        try:
            r = path.relative_to(b)
        except ValueError:
            b, old = b.parent, b
            if b == old:
                raise
            res /= '..'
        else:
            return res / r
        
cout << 'found' << compute_link(d1, d0) << endl
Reply
#3
sorry, i don't understand line 29. it looks like some odd form or variant of c++.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Sep-05-2018, 05:08 AM)Skaperen Wrote: sorry, i don't understand line 29
Then use print('found', compute_link(d1, d0)) Rolleyes
Reply
#5
so python includes some c++ stuff? what else?

* Skaperen never did like c++
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] How to resolve version conflicts in Python? atonalwilson 1 989 May-04-2023, 09:02 AM
Last Post: buran
  How to resolve version conflicts in Python? taeefnajib 0 908 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  How to resolve my problem in Pycharm? bshoushtarian 0 848 Sep-26-2022, 11:45 AM
Last Post: bshoushtarian
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,950 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,198 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,209 Aug-18-2021, 05:27 AM
Last Post: snippsat
  How to resolve Index Error in my code? codify110 6 3,011 May-22-2021, 11:04 AM
Last Post: supuflounder
  How to reference the relative directory when creating a photoimage kenwatts275 3 6,326 May-18-2021, 07:22 PM
Last Post: menator01
  How to resolve numpy ValueError: dtype.descr Py_veeran 0 1,839 Aug-18-2020, 06:46 PM
Last Post: Py_veeran
  Need help to resolve this MK2020 1 3,370 Mar-28-2020, 05:04 AM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020