I'm Brett Slatkin and this is where I write about programming and related topics. Check out my favorite posts if you're new to this site. You can also contact me here or view my projects.

25 February 2014

Saw this behavior today:

>>> mimetypes.guess_extension('text/plain')


Behold, mimetypes.py, a gem from the Python standard library:

def guess_extension(self, type, strict=True):
    """Guess the extension for a file based on its MIME type.

    Return value is a string giving a filename extension,
    including the leading dot ('.').  The extension is not
    guaranteed to have been associated with any particular data
    stream, but would be mapped to the MIME type `type' by
    guess_type().  If no extension can be guessed for `type', None
    is returned.

    Optional `strict' argument when false adds a bunch of commonly found,
    but non-standard types.
    extensions = self.guess_all_extensions(type, strict)
    if not extensions:
        return None
    return extensions[0]

If there are multiple possibilities, return one at random. In this case, a Korn Shell script.
© 2009-2019 Brett Slatkin