Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] - نسخه متنی

David Ascher, Alex Martelli, Anna Ravenscroft

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Recipe 2.20. Finding a File on the Python Search Path


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):
for dirname in sys.path:
candidate = os.path.join(dirname, pathname)
if matchFunc(candidate):
return candidate
raise Error("Can't find file %s" % pathname)
def findFile(pathname):
return _find(pathname)
def findDir(path):
return _find(path, matchFunc=os.path.isdir)


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 the previous Recipe 2.18 to find files or directories
with pathnames relative to the Python search path.


See Also


Recipe 2.18; documentation
for the os module in the Library
Reference
and Python in a
Nutshell
.


/ 394