[Rd] file.exists does not like path names ending in /

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 17 14:56:02 CET 2014


On Fri, Jan 17, 2014 at 6:16 AM, Martin Maechler
<maechler at stat.math.ethz.ch> wrote:
>>>>>> Gabor Grothendieck <ggrothendieck at gmail.com>
>>>>>>     on Fri, 17 Jan 2014 00:10:43 -0500 writes:
>
>     > If a path name ends in slash then file.exists says it does
>     > not exist.  I would have expected these to all return
>     > TRUE.
>
>     >> file.exists("/Program Files")
>     > [1] TRUE
>     >> file.exists("/Program Files/")
>     > [1] FALSE
>     >> file.exists(normalizePath("/Program Files/"))
>     > [1] FALSE
>     >> R.version.string
>     > [1] "R version 3.0.2 Patched (2013-11-25 r64299)"
>
> I would also have expected all these to work,
> but that is only because I do not use Windows;
> indeed, for me  (Linux Fedora 19, but I'm pretty sure *any*
> version):
>
>   > dir.create("/tmp/foo bar")
>   > normalizePath("/tmp/foo bar/")
>   [1] "/tmp/foo bar"
>   > file.exists("/tmp/foo bar/")
>   [1] TRUE
>   > file.exists(normalizePath("/tmp/foo bar/"))
>   [1] TRUE
>   >
>
>
>     > I am using Windows 8.1 .
>
> poor you  ;-)   yes, don't take it personally
>
> Last but not least   ?file.exists   in its  'Details' section
> mentions several times how Windows behaves differently from the
> civilized world (:-)
> notably that it also says
>
>   (However,
>      directory names must not include a trailing backslash or slash on
>      Windows.)
>
> So, all seems as documented, but unfortunately your (and most
> probably not only yours !!) expectations are different.
> I agree that this is quite unfortunate, and we all would be
> happy if OSes were consistent here.
>
> I wonder if R couldn't help you (and many others) better to
> gloss over these OS differences in a helpful way.
> This list may be a good place to discuss such proposals ...
>

I do find that sometimes I have to deal with paths that I did not
create that end in / or \.  At the moment I am using this to avoid the
problem:

File.exists <- function(x) {
   if (.Platform$OS == "windows" && grepl("[/\\]$", x)) {
       file.exists(dirname(x))
   } else file.exists(x)
}

but it would be nice if that could be done by file.exists itself.


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-devel mailing list