Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6468843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:55:39+00:00 2026-05-25T05:55:39+00:00

Say, I have two absolute paths. I need to check if the location referring

  • 0

Say, I have two absolute paths. I need to check if the location referring to by one of the paths is a descendant of the other. If true, I need to find out the relative path of the descendant from the ancestor. What’s a good way to implement this in Python? Any library that I can benefit from?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:55:39+00:00Added an answer on May 25, 2026 at 5:55 am

    os.path.commonprefix() and os.path.relpath() are your friends:

    >>> print os.path.commonprefix(['/usr/var/log', '/usr/var/security'])
    '/usr/var'
    >>> print os.path.commonprefix(['/tmp', '/usr/var'])  # No common prefix: the root is the common prefix
    '/'
    

    You can thus test whether the common prefix is one of the paths, i.e. if one of the paths is a common ancestor:

    paths = […, …, …]
    common_prefix = os.path.commonprefix(list_of_paths)
    if common_prefix in paths:
        …
    

    You can then find the relative paths:

    relative_paths = [os.path.relpath(path, common_prefix) for path in paths]
    

    You can even handle more than two paths, with this method, and test whether all the paths are all below one of them.

    PS: depending on how your paths look like, you might want to perform some normalization first (this is useful in situations where one does not know whether they always end with ‘/’ or not, or if some of the paths are relative). Relevant functions include os.path.abspath() and os.path.normpath().

    PPS: as Peter Briggs mentioned in the comments, the simple approach described above can fail:

    >>> os.path.commonprefix(['/usr/var', '/usr/var2/log'])
    '/usr/var'
    

    even though /usr/var is not a common prefix of the paths. Forcing all paths to end with ‘/’ before calling commonprefix() solves this (specific) problem.

    PPPS: as bluenote10 mentioned, adding a slash does not solve the general problem. Here is his followup question: How to circumvent the fallacy of Python's os.path.commonprefix?

    PPPPS: starting with Python 3.4, we have pathlib, a module that provides a saner path manipulation environment. I guess that the common prefix of a set of paths can be obtained by getting all the prefixes of each path (with PurePath.parents()), taking the intersection of all these parent sets, and selecting the longest common prefix.

    PPPPPS: Python 3.5 introduced a proper solution to this question: os.path.commonpath(), which returns a valid path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two basic classes, one with a ManyToMany relationship to the other:
Say I have two directory paths: C:\Shares\Apple\Orange and \\MACHINENAME\Apple\Orange Is there a way to
Say I have two sets of XML elements: <One/> <Two/> <Three/> And <A/> <B/>
Say i have two files: One is called mainFile.lua: function altDoFile(name) dofile(debug.getinfo(1).source:sub(debug.getinfo(1).source:find(.*\\)):sub(2)..name) end altDoFile(libs/caller.lua)
Say I have two web servers, one local development and one live. Under SVN
Say I have two std::set<std::string> s. The first one, old_options , needs to be
Say I have two spreadsheets. One of the spreadsheets has a table like this:
Say we have two activities, Activity1 and Activity2. In Activity1's onClick() method, we have
Say I have two Java apps that I wrote: Ping.jar and Pong.jar and they
say I have two 'modules'. For instance the hardware-interface layer of a RS-232 port

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.