get file extension python code example
Example 1: how to search for a specific file extension with python
import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
print(file)
Example 2: split filename and extension python
>>> import os
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
>>> filename
'/path/to/somefile'
>>> file_extension
'.ext'
Example 3: python get file extension from path
import os.path
extension = os.path.splitext(filename)[1]
Example 4: Python file extension
filename = input("Input the Filename: ")
f_extns = filename.split(".")
print ("The extension of the file is : " + repr(f_extns[-1]))
Example 5: os extension
>>> os.path.splitext('/a/b.c/d')
('/a/b.c/d', '')