我有一个文件x
,它出现在目录中,由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
请注意,x
缺少扩展名,但我在 ?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'sstat
call: a file will be reported as existing only if you have the permissions needed by stat. Existence can also be checked byfile.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 usefile.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.
文档中的唯一提示是访问限制可能会导致 FALSE
,并且 file.access(x)
确实是 -1
但我似乎有权访问该文件(以及其他不受影响的非常相似的文件)。
最佳答案
Windows 对文件名长度有上限(260 个字符),x
超出了该上限。缩短文件会导致 file.exists
返回 TRUE
。
关于r - 为什么 file.exists 尽管位于 list.files 中却返回 FALSE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44159751/