Using a file not in the current directory? 
Author Message
 Using a file not in the current directory?

Hello,  I have a lisp program that I would like to open a file that is
not in the same directory as the file.

So right now my file command looks like this:

(with-open-file (stream "file.dat")

When I try to make it get the file elsewhere like so:

(with-open-file (stream "/somewhere/else/rhat.stk")

*** - OPEN: file #P"/ABC/somewhereelsefile.dat" does not exist

Where /ABC/ is the directory that contains the lisp program.

Any ideas?

Thank you.
-Colin



Fri, 29 Apr 2005 07:01:49 GMT  
 Using a file not in the current directory?

Quote:

> Hello,  I have a lisp program that I would like to open a file that is
> not in the same directory as the file.

> So right now my file command looks like this:

> (with-open-file (stream "file.dat")

> When I try to make it get the file elsewhere like so:

> (with-open-file (stream "/somewhere/else/rhat.stk")

> *** - OPEN: file #P"/ABC/somewhereelsefile.dat" does not exist

> Where /ABC/ is the directory that contains the lisp program.

> Any ideas?

Plenty. :) Unlike any other language I know of, Common Lisp
differentiates between a string that can be interpreted as a path and
an actual path object.  The #P before "/ABC/somewhereelsefile.dat"
means that it's the printed representation of a pathname object, not
just a string.  Check out section 19 of the HyperSpec ("Filenames")
for a whole lot more info.

If it's a file external to your program that will always be in the
same place, you probably want to do something like:

(with-open-file (stream (make-pathname
                          :directory '(:absolute "somewhere" "else")
                          :name "rhat"
                          :type "stk"))
  ...)

Or, if you never ever ever have to worry about your program being ported
away from its current platform,

(with-open-file (stream #P"/somewhere/else/rhat.stk")
  ...)

If "rhat.stk" is part of your program (e.g., a log or data file?)?
You probably want to define a "logical host" for your program and
access the file through it.  A logical host is, to a very rough
approximation, your very own little imaginary filesystem that you
define by mapping back to a real filesystem(s).  For example:

(setf (logical-pathname-translations "MY-PROGRAM")
  '(("SRC;**;*.*.*" "/usr/local/projects/my-program/**/")
    ("DATA;*.*.*"   "/var/db/my-programs-data/")))

Would map "MY-PROGRAM;SRC;frobs;master.lisp" to
"/usr/local/projects/my-program/frobs/master.lisp" and
"MY-PROGRAM;DATA;rhat.stk" to
"/var/db/my-programs-data/rhat.stk". (Note that the last "*" in the
filename wildcard is for the version--unless you're on VMS or Genera
it probably won't be meaningful. The ";" is the directory separator in
the "logical host" syntax, and the "**" is a directory wildcard.
Using logical hosts obviously makes relocating your program a great
deal easier!

This may seem confusing--the filename bit took me the longest to "get"
and start using out of just about anything in Common Lisp, I think
because I had taken hardcoded paths for granted.  If I've been unclear,
please reply!

Gabe Garza



Fri, 29 Apr 2005 08:28:19 GMT  
 Using a file not in the current directory?

Quote:

> Hello,  I have a lisp program that I would like to open a file that is
> not in the same directory as the file.

> So right now my file command looks like this:

> (with-open-file (stream "file.dat")

> When I try to make it get the file elsewhere like so:

> (with-open-file (stream "/somewhere/else/rhat.stk")

> *** - OPEN: file #P"/ABC/somewhereelsefile.dat" does not exist

> Where /ABC/ is the directory that contains the lisp program.

> Any ideas?

First of all, the above idiom is usabel, but it has an implicit
dependency on the implementation.

By using the string "/somewhere/else/rhat.stk" (btw. the ".stk" at the
end makes me suspicious :) ) you are deferring to the implementation
dependent function PARSE-NAMESTRING to transform the string into a
pathname.  Think of the Java java.io.File class.

The error that you get is somewhat suspicious because it does not look
like any implementation I know of.  Under UNIX PARSE-NAMESTRING of the
string

         "/somewhere/else/rhat.stk"

would return something like

        #p"/somewhere/else/rhat.stk"

on most CL implementations.  Which is a shorthand for something like

        #S(PATHNAME :host <impl-dependent>
                    :device <impl-dependent, probably NIL>
                    :directory (:absolute "somewhere" "else")
                    :name "rhat"
                    :type "stk"
                    :version :newest ; or NIL
                    )

(The above is #S notation for pathanmes)

What you report looks strange.  What Common Lisp implementation and OS
combination are you using?

Cheers

--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
715 Broadway 10th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.



Sat, 30 Apr 2005 00:36:23 GMT  
 Using a file not in the current directory?

Quote:

>Hello,  I have a lisp program that I would like to open a file that is
>not in the same directory as the file.

>So right now my file command looks like this:

>(with-open-file (stream "file.dat")

>When I try to make it get the file elsewhere like so:

>(with-open-file (stream "/somewhere/else/rhat.stk")

>*** - OPEN: file #P"/ABC/somewhereelsefile.dat" does not exist

It looks like your Lisp implementation is ignoring "/" characters in
strings.  Are you sure you didn't write:

(with-open-file (stream "\somewhere\else\rhat.stk")

Backslash is an escape prefix character in strings, so if you want it to be
treated literally you must escape it:

(with-open-file (stream "\\somewhere\\else\\rhat.stk")

If you really did use the forward slashes, then it seems that your
implementation uses them as escapes as well (I think Zetalisp was like
this), and you'll need to prefix them with "\" to get them taken literally.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sat, 30 Apr 2005 00:40:12 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to retrieve current key used on the parent file, especially when using XPlore

2. Getting list of files from current directory?

3. write file to current directory

4. Getting list of file names for current directory?

5. Reading all files in the current directory

6. Reading all files in the current directory

7. Reading all files in the current directory

8. Directory of current file

9. how to list files in the current directory with some extension

10. current directory function and importing PY files?

11. Making file path control only select directory paths not file names

12. Select a directory, not a file

 

 
Powered by phpBB® Forum Software