Friday, February 25, 2011

Getting a File Extension

Here is a useful bit of code to get the file extension of a file. The os module has a useful built in function called splitext(), which returns a string array where the 2nd entry is the file extension.

Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns ('.cshrc', '').


def __fileExtension(filePath):
import os
return os.path.splitext(filePath)[1]

No comments: