Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 |
import os filename = 'Demo.csv' print("The original filename:", filename) tempTuple = os.path.splitext(filename) print("The tuple is:", tempTuple) filename = tempTuple[0] print("The output filename:", filename) |
Output:
1 2 3 4 5 |
The original filename: Demo.csv The tuple is: ('Demo', '.csv') The output filename: Demo |
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.
1 2 3 4 5 6 7 8 9 10 |
import os filename = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv' print("The original filename:", filename) tempTuple = os.path.splitext(filename) print("The tuple is:", tempTuple) filename = tempTuple[0] print("The output filename:", filename) |
Output:
1 2 3 4 5 |
The original filename: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The tuple is: ('/home/aditya1117/PycharmProjects/pythonProject/Demo', '.csv') The output filename: /home/aditya1117/PycharmProjects/pythonProject/Demo |
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.
1 2 3 4 5 6 7 8 |
import os filename = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv' print("The original filename:", filename) filename = os.path.basename(filename) print("The output filename:", filename) |
Output:
1 2 3 4 |
The original filename: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The output filename: Demo.csv |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
import os filename = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv' print("The original filename:", filename) filename = os.path.basename(filename) print("The modified filename:", filename) tempTuple = os.path.splitext(filename) print("The tuple is:", tempTuple) filename = tempTuple[0] print("The output filename:", filename) |
Output:
1 2 3 4 5 6 |
The original filename: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The modified filename: Demo.csv The tuple is: ('Demo', '.csv') The output filename: Demo |
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.
1 2 3 4 5 6 7 8 9 10 |
import os filename = 'Demo.tar.gz' print("The original filename:", filename) tempTuple = os.path.splitext(filename) print("The tuple is:", tempTuple) filename = tempTuple[0] print("The output filename:", filename) |
Output:
1 2 3 4 5 |
The original filename: Demo.tar.gz The tuple is: ('Demo.tar', '.gz') The output filename: Demo.tar |
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.
Further reading:
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.
1 2 3 4 5 6 7 8 |
import pathlib filename = pathlib.Path('Demo.csv') print("The original filename:", filename) filename = filename.stem print("The output filename:", filename) |
Output:
1 2 3 4 |
The original filename: Demo.csv The output filename: Demo |
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.
1 2 3 4 5 6 7 8 |
import pathlib filename = pathlib.Path('/home/aditya1117/PycharmProjects/pythonProject/Demo.csv') print("The original filename:", filename) filename = filename.stem print("The output filename:", filename) |
Output:
1 2 3 4 |
The original filename: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The output filename: Demo |
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.
1 2 3 4 5 6 7 8 |
import pathlib filename = pathlib.Path('Demo.tar.gz') print("The original filename:", filename) filename = filename.stem print("The output filename:", filename) |
Output:
1 2 3 4 |
The original filename: Demo.tar.gz The output filename: Demo.tar |
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.
1 2 3 4 5 6 7 8 |
import pathlib filename = pathlib.Path('Demo.tar.gz') print("The original filename:", filename) extensions=filename.suffixes print("The file extensions are:",extensions) |
Output:
1 2 3 4 |
The original filename: Demo.tar.gz The file extensions are: ['.tar', '.gz'] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
import pathlib filename = pathlib.Path('Demo.tar.gz') print("The original filename:", filename) extensions = filename.suffixes print("The file extensions are:", extensions) for extension in extensions: filename = filename.stem filename = pathlib.Path(filename) print("The output filename is:", filename) |
Output:
1 2 3 4 5 |
The original filename: Demo.tar.gz The file extensions are: ['.tar', '.gz'] The output filename is: Demo |
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