I am looking for a simple way to extract the extension (.txt, .py, .jl, etc) from a file in Julia. I looked through the Julia docs but didn't see anything specifically built for this.
I am looking for a simple way to extract the extension (.txt, .py, .jl, etc) from a file in Julia. I looked through the Julia docs but didn't see anything specifically built for this.
Use splitext from Filesystem
julia> splitext("/home/myuser/example.jl")
("/home/myuser/example", ".jl")
julia> splitext("/home/myuser/example")
("/home/myuser/example", "")
While there is no built in Julia function to do this, what you can do is given the filename, split the string on a period "." as follows:
julia> filePath = "some/path/my_program.jl"
"some/path/my_program.jl"
julia> split(filePath, ".")[2]
"jl"
This is a faster alternative, if that is of any importance:
file_extension(file::String) = file[findlast(==('.'), file)+1:end]