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?

Leave a Reply

Your email address will not be published. Required fields are marked *