'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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael K. Borregaard |
