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 7708447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:36:52+00:00 2026-06-01T00:36:52+00:00

I have a script that searches for a directory containing a specific file, starting

  • 0

I have a script that searches for a directory containing a specific file, starting from the current directory and going up the tree (think of trying to find out where the .git directory sits).

My method looks like this:

def getDir(self,cwd):
  path = os.path.abspath(cwd)
  if not os.path.isdir(path):
    raise RuntimeError("getDir should not be run on files")
  if FILE in os.listdir(path):
    return path
  parent = os.path.join(path, os.path.pardir)
  if os.access(parent, os.R_OK) and os.access(parent, os.X_OK):
    return self.getDir(parent)
  else
    return None

Now the problem with this method is that, if it cannot find the directory, it loops (and eventually stack-overflows) because apparently joining / and .. gives you / again. I tried comparing path with parent or their reprs, but that did not work (they were always distinct). My workaround for now is to include a depth counter in the recursive method and stop at some random maximum threshold.

My question is thus, is there a reliable cross-platform way to check whether I have reached a root in the file system?

  • 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-06-01T00:36:53+00:00Added an answer on June 1, 2026 at 12:36 am

    I don’t think you can find out if it’s a file system root portably, however I’d suggest doing a call to os.path.realpath() on both the current dir and your calculated parent and compare if they’re the same — this means you are spinning your wheels and there’s no point in proceeding.

    For example:

    >>> os.path.realpath('/usr/bin/..')
    '/usr'
    >>> os.path.realpath('/usr/bin/../..')
    '/'
    >>> os.path.realpath('/usr/bin/../../..')
    '/'
    >>> os.path.realpath('/..')
    '/'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery script that searches a php file that produces an XML
I'm trying to write a configure.ac file such that the resulting configure script searches
At the minute I have a page with an AJAX script that searches a
I have a script that retrieves objects from a remote server through an Ajax
I have a script that checks responses from HTTP servers using the PEAR HTTP
I have a script that takes a table name and generates a control file
I have an script that receives an encrypted url and from that generates a
I am trying to write a simple command that searches through a music directory
If I have a script that writes 1000 lines to file and then continues
I have a script that updates a CSS file based upon some user input

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.