Feature request: How about pattern matching of `Base.Filesystem.readdir`?

It’s Base.Filesystem.readdir in julia:
https://docs.julialang.org/en/v1/base/file/#Base.Filesystem.readdir

It’s readdir in R, it has exclude argument as regular expression:

readdir(dir::AbstractString=pwd();
    join::Bool = false,
    sort::Bool = true,
) -> Vector{String}

Present readdir has two arguments, how about pattern matching feature?

For example:

julia> readdir()
4-element Vector{String}:
 "Australia-1950.xlsx"
 "Australia-1955.csv"
 "Australia-1960.csv"
 "example.jl"

julia> readdir(pattern = "*.csv") # Common sense
2-element Vector{String}:
 "Australia-1955.csv"
 "Australia-1960.csv"

julia> readdir(pattern = Not("*.csv")) # What a native julia
2-element Vector{String}:
 "Australia-1950.xlsx"
 "example.jl"

julia> readdir(pattern = Not(["*.csv", "*.jl"])) # What a native julia 2
1-element Vector{String}:
 "Australia-1950.xlsx"

julia> readdir(pattern = "Australia*") # Regular expression. It could be tricky or just use RE.
3-element Vector{String}:
 "Australia-1950.xlsx"
 "Australia-1955.csv"
 "Australia-1960.csv"

Of course I can do all above examples using regular expression, not that hard, but I guess it’s gonna fantastic if the feature become native. And I think regular expression is too much to use somtimes
in these function, so it’ll be pleasure if “*.csv” thing is implemented although whole regular expression are not supported. Alternatively, extension = "csv" will be also great.

you’re just looking for

filter(!contains(r"*\.csv"), readdir())
3 Likes

No, as I mentioned, it could be implemented easily like that. Even regular expression is not nesessary. Below is not perfect, but almost works:

readdir()[contains.(readdir(), ".csv")]

My point is, an argument like pattern or extension would be a good feature of readdir. Especially newbies to julia , I think.

I think many would disagree. Composability > bloated option repetition in each function.

6 Likes

This might also be of relevance: GitHub - vtjnash/Glob.jl: Posix-compliant file name pattern matching

1 Like

Sorry to nitpick, but you probably meant:
r".*\.csv"

3 Likes

yes thanks for the correction, or

endswith(".csv")
contains(".csv")

if one doesn’t want the \. escaping in regex

1 Like