logo

How to Browse Files and Paths in Julia 📂Julia

How to Browse Files and Paths in Julia

Code

The function used to traverse a folder and obtain a list of subfolders and files is walkdir(path). Suppose a folder structure is as follows.

A/
├─ B/
│  ├─ BB/
│  │  └─ bbb.py
│  ├─ b.py
│  └─ bb.csv
├─ C/
│  ├─ CC/
│  │  └─ c.txt
│  └─ c.jl
├─ a.txt
└─ aa.py

With the following code, you can list the subfolders and files of folder A. root contains the current path, dirs contains the list of subfolders within root, and files contains the list of files within root.

julia> for (root, dirs, files) in walkdir(cd)
           println("root: $(root)")
           println("dirs: $(dirs)")
           println("files: $(files)\n")
       end
root: D:\A
dirs: ["B", "C"]
files: ["a.txt", "aa.py"]

root: D:\A\B
dirs: ["BB"]
files: ["b.py", "bb.csv"]

root: D:\A\B\BB
dirs: String[]
files: ["bbb.py"]

root: D:\A\C
dirs: ["CC"]
files: ["c.jl"]

root: D:\A\C\CC
dirs: String[]
files: ["c.txt"]

Environment

  • OS: Windows11
  • Version: Julia 1.11.3