Georgery
Georgery

Reputation: 8117

Identify Folders in Sub-Folder

I have a working directory with the following content

readdir()

6-element Vector{String}:
 "Manifest.toml"
 "Project.toml"
 "Report"
 "main.jl"
 "src"
 "test.jl"

I can identify folders here:

filter(isdir, readdir())

2-element Vector{String}:
 "Report"
 "src"

And I can show the content of the sub-folder Report:

readdir("Report/")

3-element Vector{String}:
 "jl_ACKKIu"
 "jl_zfa8Ys"
 "test.pdf"

So, why can I not identify folders in the sub-folder?

filter(isdir, readdir("Report/"))

String[]

Upvotes: 3

Views: 148

Answers (1)

fredrikekre
fredrikekre

Reputation: 10984

readdir contains paths relative to the input directory and isdir then interprets these paths as relative to the current working directory (see pwd).

Since Julia 1.4 you can pass join=true as a keyword argument to readdir, such that the returned paths are joined with the input path:

shell> tree .
.
├── DirectoryB
│   ├── DirectoryC
│   └── FileB
└── FileA

2 directories, 2 files

julia> readdir("DirectoryB")
2-element Vector{String}:
 "DirectoryC"
 "FileB"

julia> readdir("DirectoryB"; join=true)
2-element Vector{String}:
 "DirectoryB/DirectoryC"
 "DirectoryB/FileB"

julia> filter(isdir, ans)
1-element Vector{String}:
 "DirectoryB/DirectoryC"

Upvotes: 5

Related Questions