[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bug#722156: ITP: binaryornot -- Ultra-lightweight pure Python package to check if a file is binary or text.



On 08.09.2013 16:40, Vincent Bernat wrote:
> Package: wnpp
> Severity: wishlist
> Owner: Vincent Bernat <bernat@debian.org>
> 
> * Package name    : binaryornot
>   Version         : 0.1.1
>   Upstream Author : Audrey Roy <audreyr@gmail.com>
> * URL             : https://github.com/audreyr/binaryornot
> * License         : BSD
>   Programming Lang: Python
>   Description     : Ultra-lightweight pure Python package to check if a file is binary or text.
> 
> This Python package provides a function to check if a file is a text
> file or a binary file. It uses the same heuristic as file(1) by
> looking at the first 1024 bytes of the file and checks that all
> characters are printable.
> 
> 

do we need a package for that?
it is effectively:


    with open(filename, 'r') as f:
        chunk = f.read(1024)
        textchars = ''.join(map(chr, [7,8,9,10,12,13,27] + range(0x20,
0x100)))
       bool(chunk.translate(None, textchars))

and only works with utf-8 or ascii encoded files.


Reply to: