[Python-ideas] os.path.isbinary
Ryan
rymg19 at gmail.com
Fri Aug 2 07:43:36 CEST 2013
I actually got something simple working. It's only been tested on Android:
import re
def isbinary(fpath):
with open(fpath, 'r') as f:
data = re.sub(r'(^(\'|\")|(\'|\")$)', '', repr(f.read()).replace('\\n', '\n'))
binchars = re.findall(r'\\x[0123456789abcdef]{2}', data)
per = (float(len(binchars)) / float(len(data))) * 100
if int(per) == 0:
return True
else:
return False
Ryan <rymg19 at gmail.com> wrote:
>That's a pretty good idea. Or it could be like this:
>
>if fh.printable():
>
>It would have an optional argument: the number of bytes to read in.
>Default is 512. So, if we wanted 1024 bytes instead of 512:
>
>if fh.printable(1024):
>
>David Mertz <mertz at gnosis.cx> wrote:
>
>>On Wed, Jul 31, 2013 at 5:42 PM, Chris Angelico <rosuav at gmail.com>
>>wrote:
>>
>>> This sounds more like a job for a file-like object, maybe a subclass
>>> of file that reads (and buffers) the first 512 bytes, guesses
>>whether
>>> it's text or binary, and then watches everything that goes through
>>> after that and revises its guess later on.
>>
>>
>>Something like:
>>
>> if fh.read(512).isprintable():
>> do_the_ascii_stuff(fh)
>> else:
>> do_the_bin_stuff(fh)
>>
>>
>>
>>--
>>Keeping medicines from the bloodstreams of the sick; food
>>from the bellies of the hungry; books from the hands of the
>>uneducated; technology from the underdeveloped; and putting
>>advocates of freedom in prisons. Intellectual property is
>>to the 21st century what the slave trade was to the 16th.
>>
>>
>>------------------------------------------------------------------------
>>
>>_______________________________________________
>>Python-ideas mailing list
>>Python-ideas at python.org
>>http://mail.python.org/mailman/listinfo/python-ideas
>
>--
>Sent from my Android phone with K-9 Mail. Please excuse my brevity.
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130802/ce477003/attachment.html>
More information about the Python-ideas
mailing list