Remove Extension From Filename in Python

While programming in python, we often have to deal with file names. In this article, we will discuss how we can Remove Extension From Filename in python.  

How to Remove Extension From Filename in Python?

To Remove Extension From Filename in python, we can use the functions provided in the os module or the pathlib module. Let us discuss them one by one.  

Remove Extension From Filename in Python Using the os Module

Given a file name, we can remove the file extension using the os.path.splitext() function. The splitext() function takes the file name as its input argument and returns a tuple containing the file name as its first element and the file extension as its second argument. 

To remove the file extension, we can assign the first element of the tuple returned by the splitext() function to the variable containing the original filename as follows.

Output:

Instead of the filename, when we pass the entire file path to the splitext() function, it returns the entire file path as the first element of the tuple as shown below. 

Output:

To remove the file type and get only the file name in such cases, we will first use the os.path.basename() to get the file name. The os.path.basename() function takes the string containing the file name as its input argument and returns the name of the file as shown below.

Output:

After obtaining the filename, you can use the os.path.splitext() function to remove the file extension or file type from the filename in python as follows.

Output:

The os.path.splitext() method works only when the input file name has only one file extension as in ‘abc.txt’, ‘abc.pdf’, or ‘x/y/z/abc.txt’. If the file name or path has more than one extension as in ‘abc.tar.gz’, The os.path.splitext() method can only be used to remove the last extension of the file. You can observe this in the following example.

Output:

In the output, you can observe that the splitext() method is only splitting the Demo.tar.gz into Demo.tar and ‘gz’. To remove the file extension of these types of file names, we can use an iterative method, which we have discussed in the coming sections. 

Remove Extension From Filename in Python using the pathlib module

To remove the file extension from a filename using the pathlib module, we will first create a path object using the Path() method. The Path() method takes a string containing the filename as an input argument and returns a POSIX path object in UNIX-based machines or ntpath object in Windows machines. After obtaining the path object, we can remove the file extension using the stem attribute of the path object as follows.

Output:

In this approach, even if we pass the full path of the file instead of the filename, we get only the filename. You can observe this in the following example.

Output:

Again, this approach works only when the input file name has only one file extension as in ‘abc.txt’, ‘abc.pdf’, or ‘x/y/z/abc.txt’. If the file name or path has more than one extension as in ‘abc.tar.gz’, The stem attribute can only be used to remove the last extension of the file. You can observe this in the following example.

Output:

In the output, you can observe that the stem attribute only returns Demo.tar instead of Demo, which is the actual filename. To remove the file extension of these types of file names, we can use an iterative method using the suffixes attribute as discussed below.

Remove Multiple File Extensions Using pathlib Module in Python

After creating the path object using the Path() function, we can access all the extensions of the file using the suffixes attribute as follows.

Output:

Now that we have the total number of extensions in the file name, we can use a for loop to remove all the extensions from the file as follows.

Output:

In the for loop, we need to convert the filename to a path object each time after extracting the filename using the stem attribute. Otherwise, the program will run into an error. This is due to the reason that the stem attribute contains the filename as a string. We cannot use the stem attribute on a string. Therefore, we need to convert the string into a path object using the Path() method, each time we obtain the filename in the for loop.

Conclusion

In this article, we have discussed different approaches to remove file extension or file type in python. I would suggest you use the approaches with the pathlib module as the pathlib module makes it easy to handle the file path.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning

Was this post helpful?

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.