How to get file's extension in Julia

Written by Administrator on Thursday May 14, 2020

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"

3) Get substring of a filename from the position of a dot(.) till the end of a filename string:

julia> filename[32,end]

In other language we have to define the length of a file name and then get a substring providing the first and the last character of a string, but in Julia we have "end" operator, which means the last character of a string. So, we know, that we are getting substring beginning from 32-nd characted and ending with last character.

4) Combining all operations into one

Surely, we can go step by step completing all operations shown above, but there's a more elegant solution: combine all operations into one:

julia> filename = "the_most_interesting_movie_ever.avi"

println(file[findlast(isequal('.'),file):end])

".avi"

If you need to use file extension and not to print it, just define a variable and assign file extension string to it. Let's take another example:

julia> photo = "my_favourie_photo.jpeg"

println(photo[findlast(isequal('.'),photo):end])

".jpeg"

As you see, this example is universal, we actually don't care about the length of an extension: it can have 1..4 characters

Creating a function, which returns file extension

If you need to get file's extension very often, you can think about reusability of your code. In this case we can create a funtion, which return file's extension. In Julia programming language creating and use of functions is very easy:

Create a function:

function GetFileExtension(filename)
    return filename[findlast(isequal('.'),filename):end]
end

And use it:

ext = GetFileExtension("biggest_fail.mp4")
println(ext)

result:

".mp4"