Readdir return full path

How useful do you people think it would be for readdir(dir) to return the full path name of the files and directories in directory dir?

julia> path = "/home/yakir/tmp"
"/home/yakir/tmp"

julia> readdir(path)
3-element Array{String,1}:
 "/home/yakir/tmp/screenshot_2018-09-21_13:47:12.png"
 "/home/yakir/tmp/screenshot_2018-09-21_13:51:51.png"
 "/home/yakir/tmp/screenshot_2018-09-21_14:15:28.png"

Not as default, but at least as an option. Maybe something like readdir(dir; joinpath=true)?

8 Likes

Yes I like that :+1:

1 Like

You can also do joinpath.(abspath(dir), readdir(dir)) if you want this behavior.

6 Likes

I like it too. R’s list.files gives you this option, and not having it in Julia is a consistent (albeit minor) inconvenience.

1 Like

That’s exactly what I do but i imagine that many people are doing the same hence I think it’s a good suggestion.

1 Like

I’m in favour of having the option as well.

Alternately, a keyword argument would allow the best of both worlds.

julia> pwd()
"home/yakir/tmp"

julia> readdir(pwd(); abspath=true)
3-element Array{String,1}:
 "/home/yakir/tmp/screenshot_2018-09-21_13:47:12.png"
 "/home/yakir/tmp/screenshot_2018-09-21_13:51:51.png"
 "/home/yakir/tmp/screenshot_2018-09-21_14:15:28.png"
4 Likes

Yea, that’s what I meant. The default can be abspath=false and no one would need worry. The performance would be almost identical, right?

My guess is that the IO would dwarf any performance hit from the kwargs.

1 Like

Submitted.

1 Like

I literally wanted to propose the exact same feature and implementation :wink:

1 Like

Are there ever cases where you don’t eventually need a joinpath on the results? I suppose readdir() (in the cwd) is one such case, as is the interactive “what’s in this folder” case (but I use ;ls for that). I think every time I’ve done a readdir I’ve needed to do a subsequent joinpath. Of course we can’t change the default now, but adding a keyword option seems reasonable. :+1:

I’m not sure the keyword name should be abspath as the point isn’t getting an absolute path, but rather it’s to join the directory to each result so you can immediately reference the results directly from the same CWD.

2 Likes

Yeah, it shouldn’t be abspath, but rather joinpath, or just join (the path part is implied.) The shorter the better.

1 Like

I think abspath is more explicit, since if you do readdir("foo/bar", join=true), you might expect that you will get foo/bar/*. abspath on the other hand would suggest that you will get /the/absolute/path/to/foo/bar/*.

Of course, all this only holds if we want the absolute path to be returned :wink: I think in most cases it doesn’t make a difference if it’s relative or absolute…

I had assumed that the desired behaviour was to return foo/bar/*, not the absolute path. Perhaps that could be a third alternative.

Btw. I totally overlooked that all of the suggested keywords (abspath, join and joinpath) are already defined functions… :see_no_evil:

I don’t think that’s a problem. Is it?

Well, those functions will be “overwritten” inside the readdir() function’s scope.

Ah, you mean with using Base.X

I successfully procrastinated writing my PhD thesis and created a tiny PR: https://github.com/JuliaLang/julia/pull/33004

Maybe someone can help me with the unit tests, I am not able to run the test suite (I get several segfaults) :wink:

1 Like