Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 341
logankilpatrick Avatar asked Oct 06 '19 14:10

logankilpatrick


People also ask

Can a folder have sub folders?

A subfolder is a folder stored inside another folder. Subfolders help you organize your files more completely. Each subfolder should be used to store files related to each other. For example, you might have one folder for files related to a job search.

How do I change my working directory in Julia?

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


2 Answers

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
like image 118
Anshul Singhvi Avatar answered Oct 13 '22 03:10

Anshul Singhvi


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
like image 22
Michael K. Borregaard Avatar answered Oct 13 '22 05:10

Michael K. Borregaard