Did you know ... Search Documentation:
SWI-Prolog owl logo Predicate absolute_file_name/3
Availability:built-in
Source absolute_file_name(+Spec, -Absolute, +Options)
Convert the given file specification into an absolute path. Spec is a term Alias(Relative) (e.g., (library(lists)), a relative filename or an absolute filename. The primary intention of this predicate is to resolve files specified as Alias(Relative), which use file_search_path/2 to look up the possibilities for Alias. This predicate only returns non-directories, unless the option file_type(directory) is specified or the requested access is none. The result always uses the directory separator /; if the operating system uses something different, SWI-Prolog converts the file name before it makes an OS call. If you need the filename in the OS's preferred form, use prolog_to_os_filename/2. Supported Options are:
extensions(ListOfExtensions)
List of file extensions to try. Default is [’’]. For each extension, absolute_file_name/3 will first add the extension and then verify the conditions imposed by the other options. If the condition fails, the next extension on the list is tried. Extensions may be specified both as .ext or plain ext.
relative_to(+FileOrDir)
Resolve the path relative to the given directory or the directory holding the given file. Without this option, paths are resolved relative to the working directory (see working_directory/2) or, if Spec is atomic and absolute_file_name/[2,3] is executed in a directive, it uses the current source file as reference. Note that a directive using initialization/1 is executed after loading the file. This implies that such paths are resolved relative to the working directory for the toplevel file and relative to the file loading the file holding the initialization/1 directive otherwise.

Up to version 9.3.9, the system tried both the directory holding the current source and the current working directory.

access(Mode)
Imposes the condition access_file(File, Mode). Mode is one of read, write, append, execute, search, exist or none. See also access_file/2. The default is none which, if file_type is not specified as directory or regular, returns absolute file names that result from expanding aliases without inspecting the actual file system.
file_type(Type)
Defines extensions. Current mapping: txt implies [’’], prolog implies [’.pl’,’’], executable implies [’.so’,’’] and qlf implies [’.qlf’,’’]. The Type directory implies [’’] and causes this predicate to generate (only) directories. The Type regular is the opposite of directory and is the default if no file type is specified and the effective access mode is none.

The file type source is an alias for prolog for compatibility with SICStus Prolog. See also prolog_file_type/2.

file_errors(fail/error)
If error (default), throw an existence_error exception if the file cannot be found. If fail, stay silent.159Silent operation was the default up to version 3.2.6.
solutions(first/all)
If first (default), the predicate leaves no choice point. Otherwise a choice point will be left and backtracking may yield more solutions.
expand(Boolean)
If true (default is false) and Spec is atomic, call expand_file_name/2 followed by member/2 on Spec before proceeding. This is a SWI-Prolog extension intended to minimise porting effort after SWI-Prolog stopped expanding environment variables and the ~ by default. This option should be considered deprecated. In particular the use of wildcard patterns such as * should be avoided.

The Prolog flag verbose_file_search can be set to true to help debugging Prolog's search for files. See also file_search_path/2.

This predicate is derived from Quintus Prolog. In Quintus Prolog, the argument order was absolute_file_name(+Spec, +Options, -Path). The argument order has been changed for compatibility with ISO and SICStus. The Quintus argument order is still accepted.

Examples

Using absolute_file_name/3 to find files

Using absolute_file_name/3 to find files

With swipl started from /home/janw/src/swipl-devel/library/:

?- absolute_file_name('lists.pl', Abs, [access(read)]).
Abs = '/home/janw/src/swipl-devel/library/lists.pl'.

Using a search path from any directory. Using file_type(prolog) sets the extensions to search for.

?- absolute_file_name(library(lists)', Abs,
 [access(read), file_type(prolog)]).
Abs = '/home/janw/src/swipl-devel/library/lists.pl'.
?- absolute_file_name(path(ls), Abs, [access(execute)]).
Abs = '/bin/ls'.

We can create our own search path using file_search_path/2. In this example we define two places to search for data files. Search happens in the order in which file_search_path/2 generates answers.

:- multifile user:file_search_path/2.
user:file_search_path(data, MyData) :-
 expand_file_name('~/data', [MyData]).
user:file_search_path(data, '/usr/local/share/data').
?- absolute_file_name(data(myfile), Abs, [access(read)]).
Abs = '/home/janw/data/myfile'.
Serve static files from an HTTP server

Serve static files from an HTTP server

The SWI-Prolog HTTP server doesn't not server static files by default. You explicitly have to add a handler telling it to do so. The declarations below tell the server to serve files from the current directory under / on the server.

:- use_module(library(http/http_server)).
:- use_module(library(http/http_files)).
:- http_handler(root(.), http_reply_from_files('.', []), [prefix]).

Now, start the server and visit http://localhost:8080/.

?- http_server([port(localhost:8080)]).
% Started server at http://localhost:8080/
true.

Remarks

  • Note that the localhost: only binds the loopback network, avoiding to expose your website to the outside world.
  • The first argument of http_reply_from_files/3 is subject to search using absolute_file_name/3. This allows to abstract the physical location of the directory as well as virtually merge the contents of multiple physical directories.
Tags are associated to your profile if you are logged in
Tags:
LogicalCaptain said (2021年02月26日T19:27:31):0 upvotes 0 0 downvotes
Picture of user LogicalCaptain.

This is a bit of a mixture between fish and fowl

Nothing on the path needs to exist, so in that case it's a purely syntactic operation (no verification against the actual filesystem state is made):

?- absolute_file_name('/a/b/c/d',Abs).
Abs = '/a/b/c/d'.
?- absolute_file_name('/a/b/c/d',Abs,[file_type(prolog)]).
Abs = '/a/b/c/d.pl'.

But if one specifies "directory", as Wouter Beek says, a check is made:

?- absolute_file_name('////usr/local/logic/swipl/bin/../lib/swipl/bin/x86_64-linux/',Abs, [file_type(directory)]).
Abs = '/usr/local/logic/swipl/lib/swipl/bin/x86_64-linux/'.
?- absolute_file_name('////usr/local/logic/swipl/bin/../lib/swipl/bin/x86_64-linux/foo',Abs, [file_type(directory)]).
ERROR: source_sink `'////usr/local/logic/swipl/bin/../lib/swipl/bin/x86_64-linux/foo'' does not exist
?- absolute_file_name('////usr/local/logic/swipl/bin/../lib/swipl/bin/x86_64-linux/swipl',Abs, [file_type(directory)]).
ERROR: directory `'////usr/local/logic/swipl/bin/../lib/swipl/bin/x86_64-linux/swipl'' does not exist (is not a directory)

Somehow this feels lopsided.

Various

Note that this predicate does not resolve symlinks to their actual canonical path.

It also leaves a final '/', even if there is a file there matching the name:

?- absolute_file_name('/usr/local/logic/swipl/bin/../lib/swipl/bin/x86_64-linux/swipl/',Abs).
Abs = '/usr/local/logic/swipl/lib/swipl/bin/x86_64-linux/swipl/'.
Wouter Beek said (2017年10月28日T12:36:28):0 upvotes 0 0 downvotes
Picture of user Wouter Beek.

If option file_type(directory) is used the directory denoted by Spec must exist. This is even the case if option access(write) is given at the same time. Example:

?- absolute_file_name(some_dir, Dir, [access(write),file_type(directory)]).
ERROR: source_sink `some_dir' does not exist
login to add a new annotation post.

AltStyle によって変換されたページ (->オリジナル) /