added exception handling and altered script to use 'os.path.splitext'