File opening

Trent Mick trentm at ActiveState.com
Wed Apr 3 17:44:32 EST 2002


[Salim Zayat wrote]
> Hey all.  I was wondering about opening files.  Now I know you need to 
> include the path to the file in the open statement.  Now the problems 
> arises here, though.  I am writing a module that reads in a config file 
> and creates buttons from it.  So if my module is run directly, the only 
> thing you need is 
> 
> > open('./buttons.config', 'r')
> 
> But if you wanna import the module, then the path to the open statement 
> needs to be full path to the file.  Is there a fix to this?

Structure your app to place the config files in a more appropriate place,
where "appropriate" depends on the platform. On *nix this should be:
    ~/.<appname>/buttons.config
for user-specific config info and/or:
    /etc/<appname>/buttons.config
for site-specific config info.

On Windows, the "appropriate" physical location depends the flavour of
Windows and the currently set locale, but can be determined with win32api
calls (requires the Win32 Python extensions). So, for example, I have a
little app called "go" which has a user-specific configuration file
"shortcuts.xml". My app has this little function to find that config file:


def getShortcutsFile():
    """Return the path to the shortcuts file."""
    if sys.platform.startswith("win"):
        base = os.path.join(
            shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0),
            "TrentMick", "Go")
    else:
        base = os.path.expanduser("~/.go")
    return os.path.join(base, "shortcuts.xml")


Cheers,
Trent

-- 
Trent Mick
TrentM at ActiveState.com





More information about the Python-list mailing list