Skip to content

Instantly share code, notes, and snippets.

@dokterbob
Created July 18, 2012 13:25
Show Gist options
  • Save dokterbob/3136197 to your computer and use it in GitHub Desktop.
Save dokterbob/3136197 to your computer and use it in GitHub Desktop.
Guess filename extension
"""
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