Absolute_file_name/3 and path separator character vs PL_get_filename(... PL_FILE_OSPATH)

It seems that absolute_file_name/2 and absolute_file_name/3 on Windows return a file name that uses forward-slash (/) even though the Windows separator is back-slash (\). My understanding is that Windows accepts forward-slash as equivalent to back-slash in paths, but shouldn’t absolute_file_name use the OS’s preferred separator? (This is what Python’s os.path.abspath() does)

I noticed that PL_get_filename() has an option (PL_FILE_OSPATH) to convert the path to the OS’s preferred form, so it would be easy to have absolute_file_name do this. (If backwards compatibility is an issue, add an option to absolute_file_name/3 to use the OS’s preferred separator.). Also, it might be good to expose OsPath() in pl-os.c in SWI-Prolog.h, or even make it available as a predicate.
(Python has a rich set of path functions; for example, you can get the OS’s preferred separator, and also can create a file name from a list of path items.)

I don’t know exactly at what level Windows accepts /. Surely when I wrote the low-level file handling, it did not. To make sources as easily portable as possible, all file handling in Prolog is based on forward slashes. Backward slashes are also horrible as you always have to escape them :frowning: The low level OS binding translates paths we get from the OS to / and to \ just before making the OS call. That is one choice. Python apparently made the other. I think SWI-Prolog’s choice leads to less surprises. If you create a user facing application you can use prolog_to_os_filename/2 (the to is wrong :frowning: ) to provide the familiar view to the user.

Eventually Windows will adopt /, as they will adopt UTF-8. It just takes time :slight_smile:

That is there for foreign functions that get a file from Prolog and wish to talk to the OS.

There are many file name predicates. Unfortunately they are all spread around :frowning: They do most of the important stuff though. Would be great to get a standard library.

Note that the approach to use canonical OS independent representations is all around in SWI-Prolog. Notably text is internally Unicode using \n as single line separator. OS conventions are used only during I/O.