Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Is there a way to return all files within a directory and return their full path?

Tags:

julia

readdir(path)

Returns the files under path. But it doesn't return the full path, so I have to do

joinpath.(path, readdir(path))

to return the full path to the files. Ideally, I am looking for R's list.files, e.g.

list.files(dir, full.names = TRUE, patten = "*.csv")

return all csv files in dir in R. Does Julia have equivalent?

like image 654
xiaodai Avatar asked Sep 03 '19 03:09

xiaodai


People also ask

How do I get the current working directory in Julia?

See also: cd , tempdir . Set the current working directory. See also: pwd , mkdir , mkpath , mktempdir . Temporarily change the current working directory to dir , apply function f and finally return to the original directory.

How do I change directories in Julia?

The command to change working directory is cd(dir::AbstractString=homedir()).


2 Answers

In Julia 1.4 (scheduled for end of 2019) you can use the join keyword:

readdir(path; join=true)

which would be equivalent to

joinpath.(path, readdir(path))

This feature was recently merged into the Julia master branch so it is already available there, see https://github.com/JuliaLang/julia/pull/33113.

like image 80
fredrikekre Avatar answered Sep 28 '22 18:09

fredrikekre


If I understood R's list.files documentation correctly, then I would do something like this in Julia:

function list_files(path::AbstractString="."; pattern::Regex=r"", all_files::Bool=true, full_names::Bool=false)::Vector{String}
    raw"""
        list_files(path::AbstractString="."; pattern::Regex=r"", all_files::Bool=true, full_names::Bool=false) -> Vector{String}

    Returns an array of file names in the specified directory path.

    # Arguments:

    * `path::AbstractString`: Directory path to list files from, by default te current directory.
    * `pattern::Regex`: Only file names which match the regular expression will be returned.
    * `all_files::Bool`: If `false`, only the names of visible files are returned (in UNIX based OS).
                         If `true`, all file names will be returned.
    * `full_names::Bool`: If `true`, the real path is prepended to the file names.
                          If `false`, only the file names are returned.

    # Usage:

    julia> list_files()
    9-element Array{String,1}:
     ".bash_logout" 
     ".bashrc"      
     ".foo.toml"    
     ".profile"     
     "LICENSE"      
     "Manifest.toml"
     "Project.toml" 
     "README.md"    
     "demo.ipynb"

    julia> list_files("Foo")
    2-element Array{String,1}:
     ".bar.md"
     "baz.md" 

    julia> list_files(; pattern=r".*\.toml")
    3-element Array{String,1}:
     ".foo.toml"    
     "Manifest.toml"
     "Project.toml" 

    julia> list_files(; pattern=r".*\.toml", full_names=true)
    3-element Array{String,1}:
     "/home/jovyan/.foo.toml"    
     "/home/jovyan/Manifest.toml"
     "/home/jovyan/Project.toml"   

    julia> list_files(; pattern=r".*\.toml", full_names=true, all_files=false)
    2-element Array{String,1}:
     "/home/jovyan/Manifest.toml"
     "/home/jovyan/Project.toml" 

    """
    files = [file for file in readdir(path) if isfile(abspath(joinpath(path, file)))]
    if pattern != r""
        files = [m.match for m in match.(pattern, files) if m != nothing]
    end
    if !all_files && Sys.isunix()
        files = [file for file in files if !startswith(file, '.')]
    end
    if full_names
        files = realpath.(files)
    end
    return files
end

I don't know how hidden files are handled in Windows by list.files.

like image 30
HarmonicaMuse Avatar answered Sep 28 '22 16:09

HarmonicaMuse