There is a python function that detects a file extension. Take a look at os.path.splitext