Extract file extension from file path
Solution 1
This is the sort of thing that easily found with R basic tools. E.g.: ??path.
Anyway, load the tools
package and read ?file_ext
.
Solution 2
Let me extend a little bit great answer from https://stackoverflow.com/users/680068/zx8754
Here is the simple code snippet
# 1. Load library 'tools'
library("tools")
# 2. Get extension for file 'test.txt'
file_ext("test.txt")
The result should be 'txt'.
Solution 3
simple function with no package to load :
getExtension <- function(file){
ex <- strsplit(basename(file), split="\\.")[[1]]
return(ex[-1])
}
Solution 4
The regexpr above fails if the extension contains non-alnum (see e.g. https://en.wikipedia.org/wiki/List_of_filename_extensions) As an altenative one may use the following function:
getFileNameExtension <- function (fn) {
# remove a path
splitted <- strsplit(x=fn, split='/')[[1]]
# or use .Platform$file.sep in stead of '/'
fn <- splitted [length(splitted)]
ext <- ''
splitted <- strsplit(x=fn, split='\\.')[[1]]
l <-length (splitted)
if (l > 1 && sum(splitted[1:(l-1)] != '')) ext <-splitted [l]
# the extention must be the suffix of a non-empty name
ext
}
Suraj
Hi! I'm Suraj and here is a little about me, on my blog That! But How?
Updated on July 08, 2022Comments
-
Suraj almost 2 years
How can I extract the extension of a file given a file path as a character? I know I can do this via regular expression
regexpr("\\.([[:alnum:]]+)$", x)
, but wondering if there's a built-in function to deal with this? -
IRTFM over 12 yearsIt doesn't show up with
??"extensions"
although one would have expected that it would. -
Carl Witthoft over 12 years@DWin: "patience, grasshopper" :-). I would also recommend package:sos . It's very cool.
-
IRTFM over 12 yearsWitthof: Color me puzzled on two accounts; how does pkg:sos address that lack of appearance of tools::fiie_ext with ??() when a reasonable person would expect it to; and one would certainly need patience obtain value from a search strategy that delivers 20 pages with 400 hits?
-
Ben Bolker over 12 years
sos
does a full text search.??
only searches metadata (title, keywords, etc.) Furthermore, it's not that hard to skim the results. (I triedfindFn("{file extension}")
,"extract {file extension}"
, and"{extract file extension}"
, the first was best.) -
user5359531 almost 7 yearsThis would be more useful with an actual code sample
-
Carl Witthoft almost 7 years@user5359531 Did you read
?file-ext
? one-line code sample is kind of silly -
Rich Scriven over 6 yearsPlease scroll up and read the accepted answer to this question.
-
Andrii over 6 yearsThank you, Rich! I read this comment and add this code just to show how it looks in the simple code snippet. Maybe it will be helpful for someone.
-
MichaelChirico over 6 yearsThe functions
basename
anddirname
obviate some of the work here -
Mikko over 6 years@Pisca46: I would like to use a function like this in an R package. Did you write the function? If not, could you add a reference in your answer?
-
Pisca46 over 6 yearsYes, I wrote the function myself.
-
Roman Luštrik over 5 yearsCan you comment how this is an improvement over
tools::file_ext
? -
Enrique Pérez Herrero over 5 yearsYou'd better use
tools
function -
Zachary Ryan Smith over 5 yearsNote well: tools::file_ext('foo.csv.gz') return 'gz', not 'csv.gz'
-
Carl Witthoft over 5 years@ZacharyRyanSmith Well, that's because the extension in your example is "gz" so the code is doing the correct thing. There's no such concept as "double extensions"
-
Zachary Ryan Smith over 5 years@CarlWitthoft, that's true. I was just warning people who confound path suffixes with a file's extension (people like me).
-
Dannid over 4 yearsThe other comment may have been first and accepted, but it is nice to see the solution written out. The accepted answer just tells you where you find the answer. This one actually answers the question.
-
Serhii almost 3 yearsThe proposed function works incorrectly if the file contains dots in the filename. The function splits the filename and outputs the second element, while it should output the last one. For the following filename 'file.name.txt' the output is 'name', not 'txt'.
tools::file_ext
works fine. -
bers almost 3 yearsDon't use
library(tools)
when you can simply usetools::file_ext
, such as intools::file_ext("test.txt")
.