Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract file extension from file path

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?

like image 446
SFun28 Avatar asked Oct 15 '11 16:10

SFun28


People also ask

How do I separate filenames and extensions?

The function splitext() in os. path allows you to separate the root and extension of a specified file path. A tuple made up of the root string and the extension string is the function's output.

How do I get the fileName from the file path?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);

How do I extract a file type in Python?

Use the os. path Module to Extract Extension From File in Python. Use the pathlib Module to Extract Extension From File in Python.


2 Answers

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 .

like image 151
Carl Witthoft Avatar answered Sep 29 '22 18:09

Carl Witthoft


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

like image 25
Andrii Avatar answered Sep 29 '22 18:09

Andrii