Saturday
6
Mar 2004

find a file in python’s path

(5:59 pm) Tags: [How do I...]

I am building a simple web framework in Python, and I need everything to be inheritable and extinsible. This includes content. So, I am doing the brain dead thing and using Python’s sys.path to search for content that may not have been overridden in the current context. The code was hacked up from a version I found here, in the first comment. I will try to find the link and post it later. This version is greatly simplified.

find a file in Python’s sys.path
import os, sys
 
def findfile(path):
    ”"”Find the file named path in the sys.path.
    Returns the full path name if found, None if not found”"”
    for dirname in sys.path:
        possible = os.path.join(dirname, path)
        if os.path.isfile(possible):
            return possible
    return None

Popularity: 14%

Comments are closed.