How to list only files (not directories/folders) in current directory in Julia [with screenshots]

Written by Administrator on Saturday May 9, 2020

To list files in current directory and exclude counting directories (sub-directories) in current folder we just use readdir() function and then we count only files.

Let's see, what we have in a directory (files and directories):

$ ls

output:

dima@linux-zsrx:~/julia> ls
bookings        hello-world.jl
books.txt       libs
count_files.jl  list_only_files.jl

You see, that we have two directories ("libs" and "bookings") and the rest are files. Our goal is to list only files and exclude directories (folders). Let's create a file with the name list_only_files.jl in order to use it in every directory we want, and put following code into it:

files_and_dirs = readdir(pwd())  # reading files and directory

for x in files_and_dirs
        if isfile(x)            # ensuring we count only files
                println(x)      # print the name of a file
        end
end

Then let's save the file and test, if it does, what we want:

$ julia list_only_files.jl

output:

books.txt
count_files.jl
hello-world.jl
list_only_files.jl

As you see, we don't see libs and bookings directories, but only files. It means we did everything correct.

In case you want to list only files, read following article: How to list only directories (not files) in current directory in Julia [with screenshots]


In this example we used openSUSE Leap 15.1 Linux distribution and julia version 1.0.3:

openSUSE version

installed julia version in openSUSE linux