def _get_format_from_filename(file, mode):
    """Return a format string obtained from file (or file.name).

    If file already exists (= read mode), an empty string is returned on
    error.  If not, an exception is raised.
    The return type will always be str or unicode (even if
    file/file.name is a bytes object).

    """
    format = ''
    file = getattr(file, 'name', file)
    try:
        # This raises an exception if file is not a (Unicode/byte) string:
        format = _os.path.splitext(file)[-1][1:]
        # Convert bytes to unicode (raises AttributeError on Python 3 str):
        format = format.decode('utf-8', 'replace')
    except Exception:
        pass
    if format.upper() not in _formats and 'r' not in mode:
        raise TypeError("No format specified and unable to get format from "
                        "file extension: {0!r}".format(file))
    return format
def _get_format_from_filename ( file , mode ) : format = '' file = getattr ( file , 'name' , file ) try : format = _os . path . splitext ( file ) [ - 1 ] [ 1 : ] format = format . decode ( 'utf-8' , 'replace' ) except Exception : pass if format . upper ( ) not in _formats and 'r' not in mode : raise TypeError ( "No format specified and unable to get format from " "file extension: {0!r}" . format ( file ) ) return format
Return a format string obtained from file (or file.name).

    If file already exists (= read mode), an empty string is returned on
    error.  If not, an exception is raised.
    The return type will always be str or unicode (even if
    file/file.name is a bytes object).