Name of the Script

David Bolen db3l at fitlinxx.com
Sat Mar 10 01:43:04 CET 2001


Paul Moore <paul.moore at uk.origin-it.com> writes:

> How _do_ applications locate their data files without hard-coding directory
> names, or making assumptions of the current directory? (Actually, that's a
> better statement of my issue...)

I can't speak for everyone, but for myself, data files (and log files,
and other output files) in my scripts are virtually always a
configurable item.  This would include their location (or base
directory for an assumed hierarchy) and often name, except in some
cases like log files where an automatic naming scheme or suffixes are
used.

For maximum flexibility, the value is generally set in a configuration
file (which may just be a Python module that is imported), or via
command line options.

If you want to supply default values for a user, it can just be
deployed as a default configuration file that gets installed with your
application.  If you go the Python module route for a configuration
file, then it can contain executable code as well, so you can make
reference to sys.argv or __file__ if you'd really like to.  And since
you'd just import the configuration file, your only concern for its
location is that it be on the Python sys.path (which can be augmented
in various ways when you install your scripts, including .pth files).

Since the configuration file would generally be loaded very early by
any top level script in the system, the possibility of a switch in the
current directory before you can process sys.argv can be prevented.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list