(filename)
Detect encoding of `filename`, which can be a ``str`` filename, a
    ``file``-like object, or ``bytes``.
Detect encoding of `filename`, which can be a ``str`` filename, a
    ``file``-like object, or ``bytes``.
Detect encoding of filename which can be a str filename a file - like object or bytes .
def detect_encoding(filename):
    """
    Detect encoding of `filename`, which can be a ``str`` filename, a
    ``file``-like object, or ``bytes``.
    """
    # Try with Unix file utility first because it's faster (~10ms vs 100ms)
    if isinstance(filename, str) and not filename.endswith(Compression.all):
        try:
            with subprocess.Popen(('file', '--brief', '--mime-encoding',
                                   filename), stdout=subprocess.PIPE) as proc:
                proc.wait()
                if proc.returncode == 0:
                    encoding = proc.stdout.read().strip()
                    # file only supports these encodings; for others it says
                    # unknown-8bit or binary. So we give chardet a chance to do
                    # better
                    if encoding in (b'utf-8', b'us-ascii', b'iso-8859-1',
                                    b'utf-7', b'utf-16le', b'utf-16be',
                                    b'ebcdic'):
                        return encoding.decode('us-ascii')
        except OSError:
            pass  # windoze

    # file not available or unable to guess the encoding, have chardet do it
    detector = UniversalDetector()
    # We examine only first N 4kB blocks of file because chardet is really slow
    MAX_BYTES = 4 * 1024 * 12

    def _from_file(f):
        detector.feed(f.read(MAX_BYTES))
        detector.close()
        return (detector.result.get('encoding')
                if detector.result.get('confidence', 0) >= .85 else
                'utf-8')

    if isinstance(filename, str):
        with open_compressed(filename, 'rb') as f:
            return _from_file(f)
    elif isinstance(filename, bytes):
        detector.feed(filename[:MAX_BYTES])
        detector.close()
        return detector.result.get('encoding')
    elif hasattr(filename, 'encoding'):
        return filename.encoding
    else:  # assume file-like object that you can iter through
        return _from_file(filename)
def detect_encoding ( filename ) : # Try with Unix file utility first because it's faster (~10ms vs 100ms) if isinstance ( filename , str ) and not filename . endswith ( Compression . all ) : try : with subprocess . Popen ( ( 'file' , '--brief' , '--mime-encoding' , filename ) , stdout = subprocess . PIPE ) as proc : proc . wait ( ) if proc . returncode == 0 : encoding = proc . stdout . read ( ) . strip ( ) # file only supports these encodings; for others it says # unknown-8bit or binary. So we give chardet a chance to do # better if encoding in ( b'utf-8' , b'us-ascii' , b'iso-8859-1' , b'utf-7' , b'utf-16le' , b'utf-16be' , b'ebcdic' ) : return encoding . decode ( 'us-ascii' ) except OSError : pass # windoze # file not available or unable to guess the encoding, have chardet do it detector = UniversalDetector ( ) # We examine only first N 4kB blocks of file because chardet is really slow MAX_BYTES = 4 * 1024 * 12 def _from_file ( f ) : detector . feed ( f . read ( MAX_BYTES ) ) detector . close ( ) return ( detector . result . get ( 'encoding' ) if detector . result . get ( 'confidence' , 0 ) >= .85 else 'utf-8' ) if isinstance ( filename , str ) : with open_compressed ( filename , 'rb' ) as f : return _from_file ( f ) elif isinstance ( filename , bytes ) : detector . feed ( filename [ : MAX_BYTES ] ) detector . close ( ) return detector . result . get ( 'encoding' ) elif hasattr ( filename , 'encoding' ) : return filename . encoding else : # assume file-like object that you can iter through return _from_file ( filename )