logankilpatrick
logankilpatrick

Reputation: 14521

How to loop through a folder of sub folders in Julia?

I am trying to write a program in Julia that given a starting folder, will loop through all sub folders such that I can open and get the contents out of all the files in the sub folders. How can I do this in Julia ?

Ideally the code would allow for an unspecified folder depth in case I don’t know it ahead of time.

Upvotes: 8

Views: 4020

Answers (2)

Anshul Singhvi
Anshul Singhvi

Reputation: 1742

You can use walkdir, like so:

for (root, dirs, files) in walkdir("mydir")
    operate_on_files(joinpath.(root, files)) # files is a Vector{String}, can be empty
end

https://docs.julialang.org/en/v1/base/file/#Base.Filesystem.walkdir

Edit: A good thing to do here is to broadcast across the array of file paths, so that you don't need to special-case an empty array.

contents = String[]
for (root, dirs, files) in walkdir("mydir")
    # global contents # if in REPL
    push!.(Ref(contents), read.(joinpath.(root, files), String))
end

Upvotes: 19

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

Maybe write a recursive function that lists all folders and files in the dir, pushes the contents of each file to a higher-scope Array, then calls itself on each of the folders? Sth like (untested code):

function outerfun(dir)
  function innerfun!(dir, filecontents)
    for name in readdir(dir)
      if isdir(name)
        innerfun!(name, filecontents)
      else
        push!(readlines(name), filecontents)
      end
    end 
  end
  filecontents = Array{String}[]
  innerfun!(dir, filecontents)
  filecontents
end

Upvotes: 2

Related Questions