Hey guys, in this blog we will see a Python Program to Extract Extension From the File Name.
Example 1: Using splitext() method from the os module
import os file_details = os.path.splitext('/path/file.exe') print(file_details) print(file_details[1])
Output
exe
- os.path.splitext() will return two things; first will be the whole path without extension and second will be the extension.
- file_details[1] will have the extension.
Example 2: Using pathlib module
import pathlib print(pathlib.Path('/path/file.exe').suffix)
Output
exe
- pathlib.Path(‘/path/file.exe’).suffix is used to extract the suffix/extension of a path.
- In this case, the suffix/extension is ‘exe’.
Check out our other python programming examples…