# Getting inconsistent error from walkdir

**URL:** <https://discourse.julialang.org/t/getting-inconsistent-error-from-walkdir/74068>\
**Category:** New to Julia\
**Tags:** error, filesystem, iterators\
**Created:** [January 5, 2022, 1:32am UTC](https://discourse.julialang.org/t/getting-inconsistent-error-from-walkdir/74068 "2022-01-05T01:32:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nyloc](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@nyloc](https://discourse.julialang.org/u/nyloc)\
**Post date:** [January 5, 2022, 1:32am UTC](https://discourse.julialang.org/t/getting-inconsistent-error-from-walkdir/74068/1 "2022-01-05T01:32:41Z")

</div>

walkdir works for some directories and not others.

```julia
julia> file_list = readdir("dummy_dir/")
5-element Vector{String}:
 "0458899-1920x1080.jpg"
 "2nd_dir"
 "The Unbelievable Truth S01E01.mp3"
 "lena.jpg"
 "second_level"

julia> file_list = readdir("dummy_dir/second_level/")
3-element Vector{String}:
 "Everyman's Guide to Mornington Crescent-7kwmYFo927g.mp4"
 "s00e00 - Pilot - Hair, fo\$otball, cats, bees.mp3"
 "third_level"

julia> file_list = readdir("dummy_dir/2nd_dir/")
2-element Vector{String}:
 "144_RAAF_plane_list.txt"
 "3rd_dir"

julia> root, dirs, files = walkdir("dummy_dir/second_level/")
Channel{Tuple{String, Vector{String}, Vector{String}}}(0) (1 item available)

julia> root, dirs, files = walkdir("dummy_dir/2nd_dir/")
ERROR: BoundsError: attempt to access Channel{Tuple{String, Vector{String}, Vector{String}}} at index [3]
Stacktrace:
 [1] indexed_iterate(I::Channel{Tuple{String, Vector{String}, Vector{String}}}, i::Int64, state::Nothing)
   @ Base ./tuple.jl:98
 [2] top-level scope
   @ REPL[5]:1

julia> 

```

walkdir fails to open an existing directory, and what is is worse is, it works with one directory but not the other.  
anyone know what is going on?  
thanks.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 5, 2022, 9:45am UTC](https://discourse.julialang.org/t/getting-inconsistent-error-from-walkdir/74068/2 "2022-01-05T09:45:52Z")

</div>

The way you use the return value of `walkdir` is wrong: it doesn’t return a tuple of `root`, `dirs` and `files`. Instead it returns an _iterator_ of such tuples. So with the line

```julia
root, dirs, files = walkdir("dummy_dir/second_level/")

```

you will have the first `(root, dirs, files)` of the iterator in a variable `root`, the second `(root, dirs, files)` in a variable `dirs`, and the third one in `files`. This is not what you want, and it fails the tree has less than 3 directories because in that case you cannot get 3 tuples from the iterator.

---

<div class="post-metadata">

**Author:** ![nyloc](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@nyloc](https://discourse.julialang.org/u/nyloc)\
**Post date:** [January 5, 2022, 2:25pm UTC](https://discourse.julialang.org/t/getting-inconsistent-error-from-walkdir/74068/3 "2022-01-05T14:25:51Z")

</div>

it doesn’t fail because of the number of directories, it doesn’t matter what order I call walkdirit still fails on the directory “2nd\_dir” but not the directory “second\_level”.

```julia
[col@localhost Julia]$ ./julia-1.7.1/bin/julia 
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.7.1 (2021-12-22)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> root, dirs, files = walkdir("dummy_dir/2nd_dir/")
ERROR: BoundsError: attempt to access Channel{Tuple{String, Vector{String}, Vector{String}}} at index [3]
Stacktrace:
 [1] indexed_iterate(I::Channel{Tuple{String, Vector{String}, Vector{String}}}, i::Int64, state::Nothing)
   @ Base ./tuple.jl:98
 [2] top-level scope
   @ REPL[1]:1

julia> root, dirs, files = walkdir("dummy_dir/second_level/")
Channel{Tuple{String, Vector{String}, Vector{String}}}(0) (1 item available)

julia> 

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 5, 2022, 3:21pm UTC](https://discourse.julialang.org/t/getting-inconsistent-error-from-walkdir/74068/4 "2022-01-05T15:21:12Z")

</div>

Please read again my previous answer. The problem is that

```julia
root, dirs, files = walkdir("dummy_dir/2nd_dir/")

```

wants to extract 3 values from the result of `walkdir`. The variable names are misleading here, what you are actually doing is clearer if you write

```julia
dir1, dir2, dir3 = walkdir("dummy_dir/2nd_dir/")

```

Obviously this does the same thing, but now the problem is clear: you cannot extract 3 directories from `walkdir` if the tree starting at `2nd_dir/` contains only two directories.

Instead you should iterate on the result of `walkdir`. For example with a loop:

```julia
for dir in walkdir("dummy_dir/2nd_dir/")
    ... # Use the dir tuple here, which contains 3 elements: rootpath, dirs and files
end

```

You can also use [destructuring](https://docs.julialang.org/en/v1/manual/functions/#destructuring-assignment) to extract the 3 elements of each `dir` directly in the loop definition:

```julia
for (rootpath, dirs, files) in walkdir("dummy_dir/2nd_dir/")
    ... # Use directly the values of rootpath, dirs and files
end

```

---

<div class="post-metadata">

**Author:** ![nyloc](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@nyloc](https://discourse.julialang.org/u/nyloc)\
**Post date:** [January 7, 2022, 2:41pm UTC](https://discourse.julialang.org/t/getting-inconsistent-error-from-walkdir/74068/5 "2022-01-07T14:41:31Z")

</div>

Sorry, you’re right I was using walkdir wrong.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 4, 2022, 4:27pm UTC](https://discourse.julialang.org/t/getting-inconsistent-error-from-walkdir/74068/6 "2022-02-04T16:27:10Z")

</div>

Ok, result is an “[iterator](https://docs.julialang.org/en/v1/base/iterators/#Iteration-utilities)” and I can [destruct](https://docs.julialang.org/en/v1/manual/functions/#destructuring-assignment) it via a for loop.  
But I am still lost. Is there e.g. a chance to figure out the number of levels of a directory tree?  
Can I restrict the walkdir-function to a specific number of directory levels.  
And then what does this additional Channel{} do to the Tuple{}  
To figure out the number of levels of a directory tree you may count the occurrences of the  
file separator char, which is different dependent on the current OS.  
Below my attempt to count this letter in directory string (e.g. variable “root” in the proposed  
for-loop in combination with “walkdir”:

```julia
# count depth / number of levels of a path
if Sys.iswindows()
    reg_path_separator = r"\\\\"
    s_dir = raw"C:\data\julia\scripts"
else
    reg_path_separator = r"/"
    s_dir = "/media/data/julia"
end
n_path_level = length(collect(eachmatch(reg_path_separator, s_dir; overlap=false)))

```
