26 de septiembre de 2014

Get file extension from filename with Python


Several times I had to put some code together some Python code to get the extension from a filename.

It was required for it to work OK with complex extensions, like ".tar.gz", and remain functional when you have dots in the filename, like in this case: "some.filename.1.v2.tar.gz".

After trying several options, the one that did the trick was using regular expressions, wrapped in a little function.

Here the case for keeping the ".", and getting the extension in the fashion ".tar.gz" or ".png":



Here the case for not keeping the ".", and getting the extension in the fashion "tar.gz" or "png":




Et voilà! :D

No hay comentarios: