[R] list.files changed in 2.7.0

davidr at rhotrading.com davidr at rhotrading.com
Wed Feb 4 17:31:18 CET 2009


My original message referred to the double slash and using grep. 
The particular behavior on C: wasn't the issue. For example,

> list.files("C:/test1", full.names=TRUE)
[1] "C:/test1/file1" "C:/test1/file2"
> list.files("C:/test1/", full.names=TRUE)
[1] "C:/test1//file1" "C:/test1//file2"
> # Note the double slashes
> file.exists(list.files("C:/test1", full.names=TRUE))
[1] TRUE TRUE
> file.exists(list.files("C:/test1/", full.names=TRUE))
[1] TRUE TRUE
> # Well that's reassuring, but I was using grep
> grep("C:/test1/file2", list.files("C:/test1", full.names=TRUE))
[1] 2
> grep("C:/test1/file2", list.files("C:/test1/", full.names=TRUE))
integer(0)
> # bummer

In older versions of R, the last command also gave the response '2'.

While R and the OS can handle the single or double slash, grep cannot
know what I'm after.
Such unannounced changes make it harder to keep production code running
as we upgrade R versions.

It's just a fussy change, but I wondered why it was made, and whether
the behavior would change in future.

-- David


-----Original Message-----
From: henrik.bengtsson at gmail.com [mailto:henrik.bengtsson at gmail.com] On
Behalf Of Henrik Bengtsson
Sent: Tuesday, February 03, 2009 2:42 PM
To: David Reiner <davidr at rhotrading.com>
Cc: r-help at r-project.org
Subject: Re: [R] list.files changed in 2.7.0

Hi,

I've verified in "R version 2.8.1 Patched (2008-12-22 r47296)" using
both Rterm and Rgui, and

list.files(path="C:")

list the files that are in the current working directory of C: and
this *can* be changed by setwd(), that is, it does not just depend on
which directory you started R in.  The command

list.files(path="C:/")

always lists the files under the root of C:.

/Henrik


On Mon, Jan 26, 2009 at 7:48 AM,  <davidr at rhotrading.com> wrote:
> Hmm. I get exactly the same files and directories with "C:" and "C:/",
> except for the double slashes now.
> Previously the two calls to list.files gave exactly the same results.
> My current directory (getwd()) is not C:. I'm puzzled by your output.
>
> -- David
>
> -----Original Message-----
> From: henrik.bengtsson at gmail.com [mailto:henrik.bengtsson at gmail.com]
On
> Behalf Of Henrik Bengtsson
> Sent: Friday, January 23, 2009 8:36 PM
> To: David Reiner <davidr at rhotrading.com>
> Cc: r-help at r-project.org
> Subject: Re: [R] list.files changed in 2.7.0
>
> And I'm not sure that list.files("C:", full.names=TRUE) returns
> correct pathnames, because it lists the files in the current directory
> (of C:), not the root of C:. There is a difference between C: and C:/,
> and you should get:
>
> list.files("C:", full.names=TRUE)
> [1] "C:aFile.txt"
> [2] "C:anotherFile.txt"
>
> list.files("C:/", full.names=TRUE)
> [1] "C:/Documents and Settings"
> [2] "C:/Program Files"
>
> Now we get:
>
> list.files("C:", full.names=TRUE)
> [1] "C:/aFile.txt"
> [2] "C:/anotherFile.txt"
>
> list.files("C:/", full.names=TRUE)
> [1] "C://Documents and Settings"
> [2] "C://Program Files"
>
> This causes
>
> pathnames <- list.files("C:", full.names=TRUE);
> file.exists(pathnames);
>
> to return all FALSE (not expected), whereas,
>
> pathnames <- list.files("C:");
> file.exists(pathnames);
>
> returns all TRUE (expected).
>
> So, that extract slash seems to be the cause.
>
> My $.02
>
> /Henrik
>
> On Fri, Jan 23, 2009 at 2:42 PM,  <davidr at rhotrading.com> wrote:
>> I just noticed a change in the behavior of list.files from 2.6.1pat
to
>> 2.7.0
>> (I noticed it in 2.8.1 and traced back.)
>>
>> Previously, if the directory ended with a slash, and full.names=TRUE,
>> the names
>> returned had a single slash at the end of the directory,
>> but now there are two. I noticed since I was getting a list of
certain
>> files and
>> then grepping in the list for a full name formed with a single slash.
>> (The double slash would be OK if I were opening the file since the OS
>> treats double
>> slash in a path the same as a single slash.)
>>
>> I searched through the release notes, etc., and couldn't find this
>> announced.
>>
>> Try
>>> list.files("C:", full.names=TRUE)
>>> list.files("C:/", full.names=TRUE)
>>
>> Is there any chance that this could be put back to the single slash
>> behavior?
>>
>> (This was on Windows XP.)
>>
>> Thanks,
>> David L. Reiner
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list