file-exists? - test if a file exists
(import (rnrs)) ;R6RS
(import (rnrs files)) ;R6RS
(import (scheme file)) ;R7RS
This procedure returns #t if the named file exists at the time the
procedure is called and #f otherwise.
Returns a single boolean object.
;; Assuming a typical Unix-like system
(file-exists? "/dev/null")
=> #t
This procedure is used to check if a file exists before trying to perform some
operation on it. One common usage is in a Scheme implementation's library
loader, which needs to check if a base filename exists in one of multiple
paths, often trying different file extensions.
- R6RS
- Valid values for a file name include strings that name a file using the
native notation of filesystem paths on an implementation’s
underlying operating system, and may include implementation-dependent
values as well.
- R7RS
- It is an error if filename is not a string. See the next section.
This procedure is absent from R5RS and earlier reports.
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
- The wrong number of arguments was passed or an argument was outside its
domain.
- R7RS
- The assertions described above are errors. Implementations may signal an
error, extend the procedure's domain of definition to include such
arguments, or fail catastrophically.
delete-file(3scm), open-input-file(3scm),
open-output-file(3scm)
This procedure is new in R6RS and its description was copied into R7RS. It is
absent from previous reports. Early Scheme running on MacLISP could use the
procedure allfiles to check if a file exists.
Programs that check if a file exist and then immediately operate on it are prone
to race conditions. The file may have been removed immediately after this
procedure checked for its existence, or it might have been created. This is
often not a problem.