Get the File Extension using Python
To get the file extension in Python, you can use built-in functions like os.path.splitext()
or Path.suffix
from the pathlib
module. These methods allow us to extract the extension of a given filename easily.
Examples to Get the File Extension
1. Using os.path.splitext()
to Get the File Extension
In this example, we will use the os.path.splitext()
function from the os
module to extract the file extension.
main.py
import os
# File name with extension
file_name = "document.pdf"
# Extract file extension
file_extension = os.path.splitext(file_name)[1]
# Print the result
print("File Extension:", file_extension)
Explanation:
- We import the
os
module to access theos.path.splitext()
function. - The variable
file_name
holds the name of the file including the extension. os.path.splitext(file_name)
splits the file name into two parts: the name and the extension.- The
[1]
index extracts only the extension part. - We print the extracted extension.
Output:
File Extension: .pdf
2. Using pathlib.Path.suffix
to Get the File Extension
In this example, we will use the Path.suffix
property from the pathlib
module to extract the file extension.
main.py
from pathlib import Path
# File name with extension
file_name = "report.docx"
# Extract file extension using Path
file_extension = Path(file_name).suffix
# Print the result
print("File Extension:", file_extension)
Explanation:
- We import the
Path
class from thepathlib
module. - The variable
file_name
holds the file name including the extension. - We create a
Path
object and use thesuffix
property to get the file extension. - The
suffix
property returns the extension as a string, including the dot (.
). - We print the extracted extension.
Output:
File Extension: .docx
3. Handling Files Without Extensions
In this example, we will check if a file has an extension before extracting it.
main.py
import os
# File name without extension
file_name = "README"
# Extract file extension
file_extension = os.path.splitext(file_name)[1]
# Check if an extension exists
if file_extension:
print("File Extension:", file_extension)
else:
print("No extension found.")
Explanation:
- We define
file_name
as a filename without an extension. os.path.splitext(file_name)
is used to separate the file name and extension.- The
[1]
index extracts the extension, but in this case, it returns an empty string. - We use an
if
condition to check whether the extension exists. - If an extension is found, we print it; otherwise, we display a message indicating that no extension was found.
Output:
No extension found.
4. Getting Multiple File Extensions from a List
In this example, we will extract file extensions from a list of filenames.
main.py
import os
# List of filenames
files = ["data.csv", "image.png", "script.py", "notes.txt"]
# Extract file extensions
extensions = [os.path.splitext(file)[1] for file in files]
# Print the result
print("File Extensions:", extensions)
Explanation:
- We define a list of filenames with different extensions.
- We use a list comprehension to iterate over each file and extract its extension using
os.path.splitext(file)[1]
. - The extracted extensions are stored in the
extensions
list. - Finally, we print the list of extracted extensions.
Output:
File Extensions: ['.csv', '.png', '.py', '.txt']
Conclusion
There are multiple ways to extract a file extension in Python:
os.path.splitext()
: Splits filename and extracts the extension.pathlib.Path.suffix
: Provides a modern and clean approach to getting the extension.- Handling Files Without Extensions: Checking if a file has an extension before extracting it.
- Extracting Multiple Extensions: Using list comprehension for batch processing.
The recommended approach is to use pathlib.Path.suffix
for modern Python applications, while os.path.splitext()
is useful for compatibility with older versions.