| normalizePath {base} | R Documentation |
Express File Paths in Canonical Form
Description
Convert file paths to canonical form for the platform, to display themin a user-understandable form and so that relative and absolute paths canbe compared.
Usage
normalizePath(path, winslash = "\\", mustWork = NA)Arguments
path | character vector of file paths. |
winslash | the separator to be used on Windows – ignoredelsewhere. Must be one of |
mustWork | logical: if |
Details
Tilde-expansion (seepath.expand) is first done onpaths.
Where the Unix-alike platform supports it attempts to turn paths intoabsolute paths in their canonical form (no ‘./’, ‘../’ norsymbolic links). It relies on the POSIX system functionrealpath: if the platform does not have that (we know of nocurrent example) then the result will be an absolute path but mightnot be canonical. Even whererealpath is used the canonicalpath need not be unique, for examplevia hard links ormultiple mounts.
On Windows it converts relative paths to absolute paths, resolves symboliclinks, converts short names for path elements to long names and ensures theseparator is that specified bywinslash. It will match each pathelement case-insensitively or case-sensitively as during the usual namelookup and return the canonical case. It relies on Windows API functionGetFinalPathNameByHandle and in case of an error (such asinsufficient permissions) it currently falls back to theR 3.6 (andolder) implementation, which relies onGetFullPathName andGetLongPathName with limitations described in the Notes section.An attempt is made not to introduceUNC paths in presence of mapped drivesor symbolic links: ifGetFinalPathNameByHandle returns aUNC path,butGetLongPathName returns a path starting with a drive letter, Rfalls back to theR 3.6 (and older) implementation.UTF-8-encoded paths not valid in the current locale can be used.
mustWork = FALSE is useful for expressing paths for use inmessages.
Value
A character vector.
If an input is not a real path the result is system-dependent (unlessmustWork = TRUE, when this should be an error). It will beeither the corresponding input element or a transformation of it intoan absolute path.
Converting to an absolute file path can fail for a large number ofreasons. The most common are
One of more components of the file path does not exist.
A component before the last is not a directory, or there isinsufficient permission to read the directory.
For a relative path, the current directory cannot bedetermined.
A symbolic link points to a non-existent place or links form aloop.
The canonicalized path would be exceed the maximum supportedlength of a file path.
Note
The canonical form of paths may not be what you expect. For example,on macOS absolute paths such as ‘/tmp’ and ‘/var’ aresymbolic links. On Linux, a path produced by bash process substitution isa symbolic link (such as ‘/proc/fd/63’) to a pipe and there is nocanonical form of such path. InR 3.6 and older on Windows, symlinks willnot be resolved and the long names for path elements will be returned withthe case in which they are inpath, which may not be canonical incase-insensitive folders.
Examples
cat(normalizePath(c(R.home(), tempdir())), sep = "\n")