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

</>
Copy
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:

  1. We import the os module to access the os.path.splitext() function.
  2. The variable file_name holds the name of the file including the extension.
  3. os.path.splitext(file_name) splits the file name into two parts: the name and the extension.
  4. The [1] index extracts only the extension part.
  5. 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

</>
Copy
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:

  1. We import the Path class from the pathlib module.
  2. The variable file_name holds the file name including the extension.
  3. We create a Path object and use the suffix property to get the file extension.
  4. The suffix property returns the extension as a string, including the dot (.).
  5. 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

</>
Copy
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:

  1. We define file_name as a filename without an extension.
  2. os.path.splitext(file_name) is used to separate the file name and extension.
  3. The [1] index extracts the extension, but in this case, it returns an empty string.
  4. We use an if condition to check whether the extension exists.
  5. 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

</>
Copy
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:

  1. We define a list of filenames with different extensions.
  2. We use a list comprehension to iterate over each file and extract its extension using os.path.splitext(file)[1].
  3. The extracted extensions are stored in the extensions list.
  4. 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:

  1. os.path.splitext(): Splits filename and extracts the extension.
  2. pathlib.Path.suffix: Provides a modern and clean approach to getting the extension.
  3. Handling Files Without Extensions: Checking if a file has an extension before extracting it.
  4. 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.