Overview for working-with-files-in-julia

How to count files in current directory in Julia?

If you plan to count files in current directory in Julia, you can just run following code:

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

function count()
    global num =0
    for i in files_and_dirs
        if isfile(i)
            num +=1
        end
    end
end

count()

println(num)

Read more

Written by Administrator on Thursday May 14, 2020

How to get file's extension in Julia

Working with files in every programming language is almost everyday routine. While writing scripts and programs in Julia, sometimes we need to choose files with certain extension. Julia offers very simple, fast and powerful tools for operation with strings.

In order to get (or print) file's extension in Julia, we don't even need to use regular expressions (which surely will took time to learn). Another question is, that not all files have extensions. In this case we have nothing to do, because in this case file's extension will be an empty string. Next question is files with complex extensions, for example .tar.gz. We are not going to solve those types, because I cannot imagine a situation, when there's need to change .tar.gz extension. That's why, let's focus in files, which have most common extensions (1,2,3,4 - characters), for example: .c , .sh or .zip.

In our example we will combine several string operations to get the final result.

Technique is very simple:

1) Let's take on a filename with extension like a string:

julia> filename = "the_most_interesting_movie_ever.avi"

2) We are looking for a last occurance of a dot (.) and find the position of this dot (meaning, on which position stands that dot in a row of characters a filename consist of):

julia> findlast(isequal('.'),filename)
32

No we know, that dot has the 32-nd position in a string "the_most_interesting_movie_ever.avi"

Read more

Written by Administrator on Thursday May 14, 2020

How to print the length of the shortest filename in Julia

Create a file "shortest_filename.jl" and put following code into it:

files = readdir(pwd())
info = Dict{String,Integer}()
println("The shortest filename has length: ",minimum(values(info)))

List of files:

$ bookings   count_files.jl  libs  list_only_files.jl
books.txt  hello-world.jl  list_only_dirs.jl  show_longest_filename.jl

Output:

$ julia show_longest_filename.jl 
The shortest filename has length: 4

Read more

Written by Administrator on Sunday May 10, 2020

How to print the longest filename in directory in Julia

First of all, let's get a list of all filenames in current directory:

files = readdir(pwd())

Then let's make a Dictionary with filename as a key and length of the filename as a value:

info = Dict{String,Integer}()

for (n,f) in enumerate(files)
    info["$(f)"] = length(f)
end

And now we can list filenames sorted alphabetically with a length of filename in brackets:

for key in sort(collect(keys(info)))
    println("$key ($(info[key]))")
end

Result:

bookings (8)
books.txt (9)
count_files.jl (14)
hello-world.jl (14)
libs (4)
list_only_dirs.jl (17)
list_only_files.jl (18)
show_longest_filename.jl (24)

Read more

Written by Administrator on Sunday May 10, 2020

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

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 those two directories (de facto: only subdirectories/sub-folders) and exclude files. Let's create a file with the name list_only_dirs.jl in order to use it in every directory in our system, and put following code into it:

Read more

Written by Administrator on Saturday May 9, 2020

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

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:

Read more

Written by Administrator on Saturday May 9, 2020