Common Lisp - how to say if a pathname points to a regular file or a directory?
Is it possible to 'stat' a file 开发者_高级运维and find its file type - regular or directory?
Read the chapter about a portable pathname library from Peter Seibel's Practical Common Lisp book. It's available for free. It has a function file-exists-p that will return a pathname when the file exists or nil if it doesn't. The returned pathname will be in directory form if it's a directory. He also gives another function for checking if the pathname is indeed in directory form.
BTW the whole book is really worth reading so check it out if you haven't already.
CL-FAD has a function DIRECTORY-EXISTS-P which, when used in combination with PATHNAME-AS-DIRECTORY canonicalizes the pathname (prevents failure when handed a string like "/path/dir-without-trailing-slash") and achives what you're asking for.
(CL-FAD:DIRECTORY-PATHNAME-P (CL-FAD:PATHNAME-AS-DIRECTORY (PROBE-FILE "/path/missing-slash")))
I think there are several ways. probe-file
followed by checking the returned true name to determine that it has a directory name but not a filename and type should do it. e.g. for a directory
(pathname-name (probe-file filespec))
-> NIL
CLISP has a function EXT:PROBE-DIRECTORY
, which tells you whether a file exists and is a directory.
Note that this function is specific to CLISP and not standard Common Lisp.
精彩评论