从文件路径中提取文件扩展名

发布于 2024-12-09 21:31:45 字数 101 浏览 0 评论 0原文

如何提取给定文件路径的文件扩展名作为字符?我知道我可以通过正则表达式 regexpr("\\.([[:alnum:]]+)$", x) 来做到这一点,但想知道是否有一个内置函数可以处理这?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一口甜 2024-12-16 21:31:45

使用 R 基本工具很容易找到这种东西。例如:??路径。

不管怎样,加载 tools 包并读取 ?file_ext

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 .

ぺ禁宫浮华殁 2024-12-16 21:31:45

让我从 https://stackoverflow.com/users/680068/zx8754 扩展一点很好的答案,

这是简单的代码片段

  # 1. Load library 'tools'
  library("tools")

  # 2. Get extension for file 'test.txt'
  file_ext("test.txt")

结果应该是“txt”。

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'.

爱的十字路口 2024-12-16 21:31:45

简单的功能,无需加载包:

getExtension <- function(file){ 
    ex <- strsplit(basename(file), split="\\.")[[1]]
    return(ex[-1])
} 

simple function with no package to load :

getExtension <- function(file){ 
    ex <- strsplit(basename(file), split="\\.")[[1]]
    return(ex[-1])
} 
混浊又暗下来 2024-12-16 21:31:45

如果扩展名包含非 alnum,上面的正则表达式将失败(参见例如 https://en.wikipedia.org/维基/文件扩展名列表
作为替代方法,可以使用以下函数:

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

}

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

}

泼猴你往哪里跑 2024-12-16 21:31:45

仅提取不带点的文件扩展名:

tools::file_ext(fileName)

提取带点的文件扩展名:

paste0(".", tools::file_ext(fileName))

extract file extension only without dot:

tools::file_ext(fileName)

extract file extension with dot:

paste0(".", tools::file_ext(fileName))

不念旧人 2024-12-16 21:31:45

一种方法是使用 sub

s <- c("test.txt", "file.zi_", "noExtension", "with.two.ext2",
       "file.with.final.dot.", "..", ".", "")

sub(".*\\.|.*", "", s, perl=TRUE)
#[1] "txt"  "zi_"  ""     "ext2" ""     ""     ""     ""    

假设有一个点 - 如果没有扩展名,则会失败:

sub(".*\\.", "", s)
#[1] "txt"         "zi_"         "noExtension" "ext2"        ""           
#[6] ""            ""            ""           

用于比较 tools::file_ext(s) 和内部使用正则表达式的代码。

tools::file_ext(s)
#[1] "txt"  ""     ""     "ext2" ""     ""     ""     ""    

pos <- regexpr("\\.([[:alnum:]]+)$", s)
ifelse(pos > -1L, substring(s, pos + 1L), "")
#[1] "txt"  ""     ""     "ext2" ""     ""     ""     ""    

A way would be to use sub.

s <- c("test.txt", "file.zi_", "noExtension", "with.two.ext2",
       "file.with.final.dot.", "..", ".", "")

sub(".*\\.|.*", "", s, perl=TRUE)
#[1] "txt"  "zi_"  ""     "ext2" ""     ""     ""     ""    

Assuming there is a dot - which will fail in case there is no extension:

sub(".*\\.", "", s)
#[1] "txt"         "zi_"         "noExtension" "ext2"        ""           
#[6] ""            ""            ""           

For comparison tools::file_ext(s) and the code with inside used regex.

tools::file_ext(s)
#[1] "txt"  ""     ""     "ext2" ""     ""     ""     ""    

pos <- regexpr("\\.([[:alnum:]]+)
quot;, s)
ifelse(pos > -1L, substring(s, pos + 1L), "")
#[1] "txt"  ""     ""     "ext2" ""     ""     ""     ""    
说好的呢 2024-12-16 21:31:45

如果您不想使用任何额外的包,您可以尝试

file_extension <- function(filenames) {
    sub(pattern = "^(.*\\.|[^.]+)(?=[^.]*)", replacement = "", filenames, perl = TRUE)
    }

如果您喜欢神秘,您可以尝试将其用作单行表达式: sub("^(.*\\.|[^. ]+)(?=[^.]*)", "", filenames, perl = TRUE) ;-)

它适用于零个 (!)、一个或多个文件名(作为字符向量或列表)具有任意数量的点.,也适用于没有任何扩展名的文件名,它返回空字符 ""

这里是我尝试过的测试:

> file_extension("simple.txt")
[1] "txt"
> file_extension(c("no extension", "simple.ext1", "with.two.ext2", "some.awkward.file.name.with.a.final.dot.", "..", ".", ""))
[1] ""     "ext1" "ext2" ""     ""     ""     ""    
> file_extension(list("file.ext1", "one.more.file.ext2"))
[1] "ext1" "ext2"
> file_extension(NULL)
character(0)
> file_extension(c())
character(0)
> file_extension(list())
character(0)

顺便说一句,tools::file_ext() 很难找到带有非字母数字字符的“奇怪”扩展名:

> tools::file_ext("file.zi_")
[1] ""

If you don't want to use any additional package you could try

file_extension <- function(filenames) {
    sub(pattern = "^(.*\\.|[^.]+)(?=[^.]*)", replacement = "", filenames, perl = TRUE)
    }

If you like to be cryptic you could try to use it as a one-line expression: sub("^(.*\\.|[^.]+)(?=[^.]*)", "", filenames, perl = TRUE) ;-)

It works for zero (!), one or more file names (as character vector or list) with an arbitrary number of dots ., and also for file names without any extension where it returns the empty character "".

Here the tests I tried:

> file_extension("simple.txt")
[1] "txt"
> file_extension(c("no extension", "simple.ext1", "with.two.ext2", "some.awkward.file.name.with.a.final.dot.", "..", ".", ""))
[1] ""     "ext1" "ext2" ""     ""     ""     ""    
> file_extension(list("file.ext1", "one.more.file.ext2"))
[1] "ext1" "ext2"
> file_extension(NULL)
character(0)
> file_extension(c())
character(0)
> file_extension(list())
character(0)

By the way, tools::file_ext() has trouble finding "strange" extensions with non-alphanumeric characters:

> tools::file_ext("file.zi_")
[1] ""
紫瑟鸿黎 2024-12-16 21:31:45

该函数使用管道:

library(magrittr)

file_ext <- function(f_name) {
  f_name %>%
    strsplit(".", fixed = TRUE) %>%
    unlist %>%
    extract(2)
 }

 file_ext("test.txt")
 # [1] "txt"

This function uses pipes:

library(magrittr)

file_ext <- function(f_name) {
  f_name %>%
    strsplit(".", fixed = TRUE) %>%
    unlist %>%
    extract(2)
 }

 file_ext("test.txt")
 # [1] "txt"
蓝色星空 2024-12-16 21:31:45

我发现没有额外的包的最简单的方法:

FileExt <- function(filename) {
  nameSplit <- strsplit(x = filename, split = "\\.")[[1]]
  return(nameSplit[length(nameSplit)])
}

Simplest way I've found with no additional packages:

FileExt <- function(filename) {
  nameSplit <- strsplit(x = filename, split = "\\.")[[1]]
  return(nameSplit[length(nameSplit)])
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文