List All Files in a Directory in Juliya

List All Files in a Directory in Juliya

  • Julia
  • 2 mins read

To list all files in a directory in Julia, you can use the readdir() function. Here is an example:

List All Files in a Directory in Julia Example

julia> readdir(".")
4-element Array{String,1}:
 ".julia"
 ".ipynb_checkpoints"
 "Project.toml"
 "main.jl"

This will return an array of strings, each representing the name of a file in the directory. In this example, the readdir() function is called with the current directory (denoted by .) as an argument, so it will return a list of all files in the current directory.

You can also specify the path to a different directory if you want to list the files in that directory instead. For example:

julia> readdir("/path/to/directory")

This will return a list of all files in the directory at the specified path.

Note that the readdir() function only returns the names of files, not their full paths. If you want to get the full path of each file, you can use the joinpath() function with map() function to combine the directory path with the file name. Here is an example:

julia> files = readdir(".")
4-element Array{String,1}:
 ".julia"
 ".ipynb_checkpoints"
 "Project.toml"
 "main.jl"

julia> full_paths = map(file -> joinpath(".", file), files)
4-element Array{String,1}:
 "./.julia"
 "./.ipynb_checkpoints"
 "./Project.toml"
 "./main.jl"

In this example, the joinpath() function is called with the current directory (.) and the list of file names returned by readdir(). This creates a new array of strings, containing the full path of each file in the directory.