How to find subsidiary files?

Andy Jewell andy at wild-flower.co.uk
Fri Mar 21 15:08:54 EST 2003


On Friday 21 Mar 2003 7:19 am, Michael Saavedra wrote:
> I am working on a gui app, and I'm having trouble figuring out how to find
> and load subsidiary files such as toolbar images and the like. The app is
> written to run on both windows and linux. It will be distributed as source
> code and in binary form using py2exe. I will not have any control over
> where in the directory tree the app will be installed.
>
> Here's the problem: on linux, executables are usually put in /usr/bin,
> /usr/local/bin, or somewhere in the /opt directory, with the other files in
> another dir such as /usr/share. On windows, the app and the other files are
> usually all stored in their own directory under C:\Program Files, though
> sometimes people use other locations, such as on the D:\ drive. How can I
> get the app to find the files at run-time in a cross-platform way?
>
> Are the distutils sufficient for doing this sort of thing? I've been
> looking at the documentation, and they seem to be more oriented toward
> distributing modules rather than apps. For the source code, I suppose I
> could modify it on installation to have a variable point to the proper
> directory, but this wouldn't work for the .exe distribution. Also, I
> considered using sys.argv[0] on linux to attempt to divine the base
> installation dir, but using symlinks to the executable would break this.
> Anyone have any other ideas?
>
> Thanks in advance,
> Mike

The *windows* way of doing this would be to store the locations in the
registry: module winreg

The *unix* way would be to have a .conf file somewhere, usually in the
directory where the main application lives.

I had a similar problem, and chose to use the *old* windows way: .ini files.
I store this in the same directory as the main file, and open it without a
path, ie from the 'current' directory.  I augmented the .ini syntax with
python expressions for more power.

Ini files have the structure:

[section]
	key=value

You can have many sections, each with many keys.  Section names and key names
are arbitrary - of your choice.

My Ini module opens a .ini file and returns a dictionary in the form:

{"section":{"key":value}}

Programs may then use:

conf["section"]["key"] to query values.


I relaxed the rules about comments.  Errors are simply ignored, and are not
reported.  You may want to change this.

Here's the whole module (I'm not saying it's perfect, but it does the job for
me):

-andyj


# Ini file handling
# By Andy Jewell - use freely, but keep this comment, please

def load(file):
    " ini file -> dictionary "

    f=open(file,"r")
    lines=f.readlines()
    f.close()
    section=""
    dictionary={}
    for line in lines:
        # Trim off comments
        if line.find("#")>-1:
            line=line[:line.find("#")]
        line=line.strip()
        if not line:
            pass
        elif section and (line.find("=")>-1):
            key,value=line.split("=")
            try:
                dictionary[section][key]=eval(value)
            except:
                dictionary[section][key]=None
        elif line.startswith("[") and line.endswith("]"):
            section=line[1:-1]
            if section:
                dictionary[section]={}
    return dictionary


def save(dictionary,file):
    """ Saves dictionary to file in ini format. Destroys comments and
         section/key order.
    """
    f=open(file,"w")
    for section in dictionary.keys():
        f.write("["+section+"]\n")
        for key in dictionary[section].keys():
            f.write("\t%s=%s\n" % (key,dictionary[section][key]))
        f.write("\n")






More information about the Python-list mailing list