How To Remove File Extension From Path String In Python: One-Liner

How do you remove the file extension from a path in Python? And can you do it using just one line of code?

The file extension is generally the last set of characters after the final period in a path string. Removing the file extension helps with trying to either rename the file name or with renaming the file extension.

For example, if my full path string to a particular file on my computer is /usr/ryan/Documents/file.csv the file extension string is .csv .

Some examples of times where I’ve sought to use this technique have been when trying to rename the file extension from something like txt to csv or vice versa, or times when I’ve completely typed the wrong extension such as text instead of txt .

The technique involved in removing the file extension from a path string is to firstly find the final period in the string and secondly to slice and keep all characters up to that string.

Find Last Character In String With Multiple Same Characters

How do you find the location of a character within a string in Python? And how can you find the location of a character if there are multiple same characters in the string?

Python has a built-in string method .find(character, [start, end]) that helps to provide the index number for the location of a character. However, it only locates the first instance of the character .

If you wanted to find the location of a character that occurs multiple times in the string then you would need to loop through the source string and use the start parameter until you received a -1 result. This process could look something like this:

from typing import List

def find_chars(source_str: str, find_char: str) -> List[int]:
    result: List[int] = []
    char_idx: int = -1
    while (char_idx := source_str.find(find_char, char_idx + 1)) > -1:
    return result

The above function find_chars(source_str, find_char) uses a while loop and a walrus operator that enables re-use of the result each time the character is found in the source string (notice that the char_idx variable is in parentheses, without this the char_idx would capture the boolean result of source_str.find() > -1 ). And each time a result is found using the .find() string method it appends the index location into the result list and once the .find() method has exhausted looping through the source string and cannot find any more characters it returns -1 which will end the while loop and enable the return of the result list.

Here’s what this function would return with a couple of examples:

>>> my_path = "/usr/ryan/Documents/file.txt"
>>> find_chars(my_path, ".")
>>> my_path = "/usr/ryan/Documents/file.main.txt"
>>> find_chars(my_path, ".")
[24, 29]

As you can see this function works as desired by helping to find the periods "." within a file path string.

Another way of obtaining a list of all the index positions in a string of a specific character is to use a list comprehension with a condition that will loop through each character in the original string and when the condition is met store the index number.

This would look something like this:

[idx for idx, x in enumerate(my_string) if x == '.']

Here’s an example using the list comprehension code above:

>>> my_path = '~/my/'
>>> idx_dots = [idx for idx, x in enumerate(my_path) if x == '.']
>>> print(idx_dots)
[9, 13]

As you can see from the above code the list comprehension produces a list of all the indexes where the period is found in the path. In this case, the periods are found at 9 and 13 .

To obtain the last index from this list you can use the built-in function max() which can take a list as its sole parameter and return the highest value.

>>> max_idx = max(idx_dots)
>>> print(max_idx)

Therefore, whether you use the custom function above to find all the period characters in a path string or the list comprehension with if condition to get the last character position in a string where multiple characters exist use the max() built-in function to get your result.

Remove File Extension Using Slice Operator

Once you have the index location of the final period in your path or file string then you can use the slice operator to capture all contents up to the index location which means the remaining characters in the string are removed.

Here’s how this would look:

>>> my_path = "/usr/ryan/Documents/file.main.txt"
>>> idx_dots = [idx for idx, x in enumerate(my_path) if x == '.']
>>> max_idx = max(idx_dots)
>>> my_path[:max_idx]

As you can see from the code above I have removed the file extension .txt from the path string.

To make this one line of code would require wrapping it all up like this:

my_path[:max([idx for idx, x in enumerate(my_path) if x == '.'])]

Where my_path is the original string containing the path or file name.


To remove a file extension from a path string find the last period in the string and remove both it and all characters after it.

To achieve this using one line of code in Python without importing any libraries use a slice operator on the original string, the built-in max() function on a list comprehension that iterates through each character in the path string capturing the index number of each period.

The one liner looks like this:

my_str[:max([idx for idx, x in enumerate(my_str) if x == '.'])]

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.