Created
July 18, 2012 13:25
-
-
Save dokterbob/3136197 to your computer and use it in GitHub Desktop.
Guess filename extension
This file contains hidden or 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
""" | |
Open files without extenions and guess mimetype through python-magic. | |
""" | |
# Fix extensions | |
(root, ext) = path.splitext(full_path) | |
if ext == '': | |
import magic, mimetype | |
mime_guesser = magic.Magic(mime=True) | |
mimetype = mime_guesser.from_file(full_path) | |
ext = mimetypes.guess_extension(mimetype) | |
if not ext: | |
logger.warning("No filetype could be determined for '%s', skipping.", | |
old_value | |
) | |
return None | |
logger.warning("No extension for '%s', guessed '%s'.", | |
old_value, ext | |
) | |
full_path = root + ext |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment