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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stef Mientki

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

    hello,

    I (again) wonder what's the perfect way to store, OS-independent,
    filepaths ?
    I can think of something like:
    - use a relative path if drive is identical to the application (I'm
    still a Windows guy)
    - use some kind of OS-dependent translation table if on another drive
    - use ? if on a network drive

    I'm interested what you all use for this kind of problem.
    And I wonder why there isn't a standard solution / library in Python
    available.

    thanks,
    Stef Mientki
  • drobinow@gmail.com

    #2
    Re: What's the perfect (OS independent) way of storing filepaths ?

    On Oct 19, 8:35 am, Stef Mientki <stef.mien...@g mail.comwrote:
    I (again) wonder what's the perfect way to store, OS-independent,
    filepaths ?
    I don't think there is any such thing. What problem are you trying to
    solve?

    Comment

    • Steven D'Aprano

      #3
      Re: What's the perfect (OS independent) way of storing filepaths ?

      On Sun, 19 Oct 2008 14:35:01 +0200, Stef Mientki wrote:
      hello,
      >
      I (again) wonder what's the perfect way to store, OS-independent,
      filepaths ?
      "Perfect"? I can't imagine any scheme which will work on every imaginable
      OS, past present and future.

      However, in practice I think there are two common forms still in use:
      Posix paths, and Windows paths. I believe that OS/2 can deal with Windows
      pathnames, and Mac OS X uses Posix paths (I think...). If you have to
      support Classic Mac OS or other non-Posix systems, then your life will
      become interesting and complicated.

      And let's not even consider Unicode issues...

      You might find this page useful:
      http://en.wikipedia.org/wiki/Path_(computing)

      Note that raw strings are for regular expressions, not Windows paths. Raw
      strings can't end in a backslash, so you can't do this:

      r'C:\My Documents\'

      Instead, you can avoid having to escape backslashes by taking advantage
      of the fact that Windows will accept forward slashes as well as
      backslashes as path separators, and write 'C:/My Documents/' instead.

      I assume you're familiar with the path-manipulation utilities in os.path?
      >>import os
      >>os.path.split drive('C://My Documents/My File.txt')
      ('C:\\\\', 'My Documents\\My File.txt')

      I had to fake the above output because I'm not running Windows, so excuse
      me if I got it wrong.

      But honestly, I think your biggest problem isn't finding a platform-
      independent way of storing paths, but simply translating between each
      OS's conventions on where files should be stored.

      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!



      --
      Steven

      Comment

      • Duncan Booth

        #4
        Re: What's the perfect (OS independent) way of storing filepaths ?

        Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
        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.normp ath(os.path.exp anduser('~/.appname'))
        'C:\\Documents and Settings\\Dunca n\\.appname'

        Comment

        • Duncan Booth

          #5
          Re: What's the perfect (OS independent) way of storing filepaths ?

          Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
          >>>import os
          >>>os.path.spli tdrive('C://My Documents/My File.txt')
          ('C:\\\\', 'My Documents\\My File.txt')
          >
          I had to fake the above output because I'm not running Windows, so
          excuse me if I got it wrong.
          Not that it matters, but:
          >>os.path.split drive('C://My Documents/My File.txt')
          ('C:', '//My Documents/My File.txt')

          Comment

          • Grant Edwards

            #6
            Re: What's the perfect (OS independent) way of storing filepaths ?

            On 2008-10-19, Stef Mientki <stef.mientki@g mail.comwrote:
            I (again) wonder what's the perfect way to store, OS-independent,
            filepaths ?
            The question appears to me to be meaningless. File paths are
            not OS independant, so an OS-independant way to store them
            doesn't seem to be a useful thing to talk about.

            --
            Grant

            Comment

            • Eric Wertman

              #7
              Re: What's the perfect (OS independent) way of storing filepaths ?

              >I (again) wonder what's the perfect way to store, OS-independent,
              >filepaths ?
              I'm in agreement that perfect probably isn't applicable. If I were
              doing this myself, I might store the information in a tuple:

              base = 'some root structure ('/' or 'C')
              path = ['some','set','o f','path','name s']
              filename = 'somefile.ext'

              pathdata = (root,path,file name)

              and write a couple of simple functions to reconstruct them based on the os.

              Comment

              • Stef Mientki

                #8
                Re: What's the perfect (OS independent) way of storing filepaths?

                Eric Wertman wrote:
                >>I (again) wonder what's the perfect way to store, OS-independent,
                >>filepaths ?
                >>>
                >
                I'm in agreement that perfect probably isn't applicable. If I were
                doing this myself, I might store the information in a tuple:
                >
                base = 'some root structure ('/' or 'C')
                path = ['some','set','o f','path','name s']
                filename = 'somefile.ext'
                >
                pathdata = (root,path,file name)
                >
                and write a couple of simple functions to reconstruct them based on the os.
                >
                Eric, I like your idea.
                It looks like a workable technique,
                the user should initial define the roots once and everything works.
                It should even work for network drives and websites.

                Duncan, in windows it's begin to become less common to store settings in
                Docs&Settings,
                because these directories are destroyed by roaming profiles (a big
                reason why I can't run Picassa ;-(
                It's more common to follow the portable apps approach, store them in the
                application directory.

                Drobinow, I want to distribute an application with a large number of
                docs and examples.
                Now for this application I can put everything in subpaths of the
                main-application,
                but you triggered me to put a warning in my code if I go outside the
                application path.
                Another application I've in mind, is a data manager (now written in Delpi),
                in which I organize all my information: docs, websites, measurement data
                etc.

                Others, thank you for the ideas, you learende me some new os.path functions.

                cheers,
                Stef

                Comment

                • Aaron Brady

                  #9
                  Re: What's the perfect (OS independent) way of storing filepaths ?

                  Duncan Booth wrote:
                  Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
                  >
                  >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.norm path(os.path.ex panduser('~/.appname'))
                  'C:\\Documents and Settings\\Dunca n\\.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.


                  Comment

                  • Steven D'Aprano

                    #10
                    Re: What's the perfect (OS independent) way of storing filepaths ?

                    On Sun, 19 Oct 2008 15:40:32 +0000, Duncan Booth wrote:
                    Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
                    >
                    >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.norm path(os.path.ex panduser('~/.appname'))
                    'C:\\Documents and Settings\\Dunca n\\.appname'

                    Except Windows users will be wondering why they have a directory starting
                    with '.' in their home directory. Dot to make files hidden is not AFAIK
                    supported by Windows.



                    --
                    Steven

                    Comment

                    • Steven D'Aprano

                      #11
                      Re: What's the perfect (OS independent) way of storing filepaths ?

                      On Sun, 19 Oct 2008 20:50:46 +0200, Stef Mientki wrote:
                      Duncan, in windows it's begin to become less common to store settings in
                      Docs&Settings,
                      because these directories are destroyed by roaming profiles
                      Isn't *everything* destroyed by roaming profiles? *wink*

                      Seriously, I don't know anyone who has anything nice to say about roaming
                      profiles.

                      (a big
                      reason why I can't run Picassa ;-(
                      It's more common to follow the portable apps approach, store them in the
                      application directory.
                      User config files in a global directory? That bites.



                      --
                      Steven

                      Comment

                      • Duncan Booth

                        #12
                        Re: What's the perfect (OS independent) way of storing filepaths ?

                        Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
                        On Sun, 19 Oct 2008 15:40:32 +0000, Duncan Booth wrote:
                        >
                        >Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
                        >>
                        >>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.nor mpath(os.path.e xpanduser('~/.appname'))
                        >'C:\\Documen ts and Settings\\Dunca n\\.appname'
                        >
                        >
                        Except Windows users will be wondering why they have a directory
                        starting with '.' in their home directory. Dot to make files hidden is
                        not AFAIK supported by Windows.
                        >
                        The leading dot doesn't make the files hidden on Windows, but there's no
                        reason why you can't create files/folders with a leading dot and many
                        programs do just that. On the machine I'm on right now, 'dir .*' shows me:

                        Directory of C:\Documents and Settings\Duncan

                        18/10/2008 15:48 <DIR .
                        18/10/2008 15:48 <DIR ..
                        22/09/2008 20:28 45 .appcfg_nag
                        30/07/2004 09:28 244 .bashrc
                        18/01/2007 10:50 6,783 .bash_history
                        11/06/2006 12:30 <DIR .borland
                        06/04/2004 15:43 365 .emacs-places
                        11/03/2004 12:34 <DIR .emacs.d
                        08/01/2007 19:35 30,390 .fonts.cache-1
                        03/05/2008 18:07 <DIR .gimp-2.0
                        14/01/2005 14:34 <DIR .idlerc
                        11/06/2006 12:36 <DIR .jbuilder2005
                        27/06/2005 12:44 3 .leoID.txt
                        11/06/2006 12:43 <DIR .Nokia
                        11/06/2006 12:36 <DIR .primetime2005
                        11/06/2006 12:13 <DIR .qualitycentral
                        04/02/2004 12:24 <DIR .ssh
                        12/08/2004 14:30 <DIR .thumbnails
                        30/07/2004 14:11 49 .Xauthority
                        29/07/2004 16:25 12 .Xdefaults
                        29/07/2004 14:36 <DIR .xemacs
                        17/08/2006 09:19 2,869 .XWinrc

                        not to mention the thousands of .svn folders scattered across the rest of
                        the system.

                        Comment

                        • Steven D'Aprano

                          #13
                          Re: What's the perfect (OS independent) way of storing filepaths ?

                          On Sun, 19 Oct 2008 20:50:46 +0200, Stef Mientki wrote:
                          Duncan, in windows it's begin to become less common to store settings in
                          Docs&Settings,
                          because these directories are destroyed by roaming profiles (a big
                          reason why I can't run Picassa ;-(
                          It's more common to follow the portable apps approach, store them in the
                          application directory.
                          What do you do when users don't have write permission for the application
                          directory?


                          --
                          Steven

                          Comment

                          • Ross Ridge

                            #14
                            Re: What's the perfect (OS independent) way of storing filepaths ?

                            Stef Mientki wrote:
                            >Duncan, in windows it's begin to become less common to store settings in
                            >Docs&Setting s,
                            >because these directories are destroyed by roaming profiles (a big
                            >reason why I can't run Picassa ;-(
                            >It's more common to follow the portable apps approach, store them in the
                            >application directory.
                            Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
                            >What do you do when users don't have write permission for the application
                            >directory?
                            Use the application data directory (if you want it to roam), or the
                            local application data directory (if not). However, the normal place
                            to store settings on Windows is in the registry.

                            Ross Ridge

                            --
                            l/ // Ross Ridge -- The Great HTMU
                            [oo][oo] rridge@csclub.u waterloo.ca
                            -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
                            db //

                            Comment

                            • Steven D'Aprano

                              #15
                              Re: What's the perfect (OS independent) way of storing filepaths ?

                              On Mon, 20 Oct 2008 10:20:06 +0000, Duncan Booth wrote:
                              Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
                              >
                              >On Sun, 19 Oct 2008 15:40:32 +0000, Duncan Booth wrote:
                              >>
                              >>Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
                              >>>
                              >>>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
                              >>>convention s!
                              >>>
                              >>The first of those should do nicely for both Linux and Windows:
                              >>>
                              >>>>>os.path.no rmpath(os.path. expanduser('~/.appname'))
                              >>'C:\\Document s and Settings\\Dunca n\\.appname'
                              >>
                              >>
                              >Except Windows users will be wondering why they have a directory
                              >starting with '.' in their home directory. Dot to make files hidden is
                              >not AFAIK supported by Windows.
                              >>
                              The leading dot doesn't make the files hidden on Windows, but there's no
                              reason why you can't create files/folders with a leading dot and many
                              programs do just that. On the machine I'm on right now, 'dir .*' shows
                              me:
                              [snip]

                              And 75% [1] of average Windows users will either delete the file, move it
                              to a more convenient[2] location, or edit the file name to remove the dot.

                              I'm not saying you can't use files with a leading period under Windows.
                              You can name all your config files using Morse code if you want. I'm
                              saying it breaks the expected Windows file location guidelines, and it
                              displays a shameful lack of care by the programmer. Windows users are
                              more forgiving of badly behaved programs than Macintosh users, but even
                              so, it's pretty shoddy to ignore a platform's standard file locations.
                              Unix/Linux coders would rightly be annoyed if some tool created a
                              directory "/Program Files" instead of using the appropriate /bin
                              directory. This is no different.





                              [1] Based on my experience of the typical Windows user. YMMV.

                              [2] And they are right to do so. Programs that dump config files and
                              directories, hidden or not, in the top level of the user's home directory
                              are incredibly rude. It may have been a Unix standard for as long as
                              there has been a Unix, but it's still the programming equivalent of
                              coming into somebody's house and throwing your tools all over their
                              living room floor.



                              --
                              Steven

                              Comment

                              Working...