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":
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def get_extension(filename): | |
regex = re.compile(r'^.*?.(?P<ext>.tar\.gz|.tar\.bz2|.\w+)$') | |
return regex.match(filename).group('ext') |
Here the case for not keeping the ".", and getting the extension in the fashion "tar.gz" or "png":
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def get_extension(filename): | |
regex = re.compile(r'^.*?[.](?P<ext>tar\.gz|tar\.bz2|\w+)$') | |
return regex.match(filename).group('ext') |
Et voilà! :D
No hay comentarios:
Publicar un comentario