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

Robert Brown bbrown at speakeasy.net
Thu Oct 23 19:24:07 EDT 2008


Stef Mientki <stef.mientki at gmail.com> writes:

> 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

There is no perfect solution, since file names and semantics differ from one
operating system to the next.  Genera, the Lisp Machine operating system, has
facilities to abstract over the details of the file systems in use in the
1980s: Multics, ITS, TOPS-20, VMS, Unix, etc.  Many of the concepts were
incorporated into the Common Lisp standard.  Here are a couple of references:

  http://gigamonkeys.com/book/files-and-file-io.html#filenames
  http://www.lispworks.com/documentation/HyperSpec/Body/19_.htm

The system described is not simple.  Briefly, there's a machine-independent
way (logical pathnames) to specify file names that a program can use to
manipulate the files it knows about.  There's no guarantee that you can access
an arbitrary file with these names.  However, there's also the concept of a
machine-specific file namestring.  Users can type in these machine-specific
namestrings, allowing the code to access arbitrary files.

Both types of pathnames can be manipulated via an API to derive other file
names.  Here's how I create a pathname that refers to a subdirectory of my
home directory:

  (merge-pathnames
    (make-pathname :directory '(:relative ".sbcl" "systems"))
    (user-homedir-pathname))

The code should work so long as the target file system supports
subdirectories, as Windows and Unix do.

bob



More information about the Python-list mailing list