What's the perfect (OS independent) way of storing filepaths ?

Aaron Brady castironpi at gmail.com
Sun Oct 19 14:55:53 EDT 2008


Duncan Booth wrote:

> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:
>
>> In Linux, config files should go into:
>> 
>> ~/.<appname>/ or /etc/<appname>/
>> 
>> In Windows (which versions?) then should go into the Documents And 
>> Settings folder, where ever that is.
>> 
>> There's no single string which can represent both of these conventions!
>
> The first of those should do nicely for both Linux and Windows:
>
>>>> os.path.normpath(os.path.expanduser('~/.appname'))
> 'C:\\Documents and Settings\\Duncan\\.appname'

A tuple of path elements, I would think.

>>> a= ( 'c:', 'windows', 'system' )
>>> a= ( '~', 'usr', 'bin' )
>>> a= ( '..', 'src' )

You'll want a subclass too, which has a file for the last name, instead 
of just folders.

>>> a= ( 'c:', 'python', 'python.exe' )

If '..' and '~' aren't universally, recognized, you'll want special
flags.

>>> DirUp= type( 'DirUp', (object,), { '__repr__': ( lambda self: 'DirUp' ) } )()
>>> a= ( DirUp, 'src' )
>>> a
(DirUp, 'src')

As for rendering them, 'win', 'unix', and 'mac' could be methods.





More information about the Python-list mailing list