文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- 218.25.66.198 [DateTime(2004-09-29T04:52:50Z)] TableOfContents

描述

Credit: Mitch Chapman

问题 Problem

A large Python application includes resource files (e.g., Glade project files, SQL templates, and images) as well as Python packages. You want to store these associated files together with the Python packages that use them.

解决 Solution

You need to be able to look for either files or directories along Python's sys.path:

import sys, os

class Error(Exception): pass

def _find(pathname, matchFunc=os.path.isfile):

def findFile(pathname):

def findDir(path):

讨论 Discussion

Larger Python applications consist of sets of Python packages and associated sets of resource files. It's convenient to store these associated files together with the Python packages that use them, and it's easy to do so if you use this variation on Recipe 4.21 to find files or directories with pathnames relative to the Python search path.

参考 See Also

Recipe 4.21; documentation for the os module in the Library Reference