Hugh
Hugh

Reputation: 16090

Why does file.exists return FALSE despite being in list.files?

I have a file x which appears in a directory, as proved by list.files

x <- "./data-raw/paths/calculate-route-by-FROM-TO/2017-05-2075--FROM-Flinders-Street-Railway-Station--Melbourne-Victoria-3004--VIC--TO-Melbourne-Sports-Aquatic-Centre--30-Aughtie-Dr--Melbourne-VIC-3206--VIC-csv"
x %in% list.files("./data-raw/paths", recursive = TRUE, full.names = TRUE)
# [1] TRUE
file.exists(x)
# [1] FALSE

Note that x lacks an extension, but I could not find such a caveat in ?file.exists:

file.exists returns a logical vector indicating whether the files named by its argument exist. (Here ‘exists’ is in the sense of the system's stat call: a file will be reported as existing only if you have the permissions needed by stat. Existence can also be checked by file.access, which might use different permissions and so obtain a different result. Note that the existence of a file does not imply that it is readable: for that use file.access.) What constitutes a ‘file’ is system-dependent, but should include directories. (However, directory names must not include a trailing backslash or slash on Windows.) Note that if the file is a symbolic link on a Unix-alike, the result indicates if the link points to an actual file, not just if the link exists. Lastly, note the different function exists which checks for existence of R objects.

The only hint from the documentation was that access restrictions might cause FALSE, and indeed file.access(x) is -1 but I appear to have access to that file (as well as other very similar files which are not so affected).

Upvotes: 2

Views: 3031

Answers (1)

Hugh
Hugh

Reputation: 16090

Windows has an upper limit on the length of a filename (260 characters), which x exceeded. Shortening the file resulted in file.exists returning TRUE.

Upvotes: 4

Related Questions