determine file type

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Mar 27 16:20:02 CEST 2006


On Sun, 26 Mar 2006 19:52:32 -0700, Mark Gibson wrote:

> 
>> 
>> 
>> import os
>> def test_file(filename, maxread=1024):
>>    if os.path.isdir(filename):
>>      return 'directory'
>>    afile = open(filename) # open as text
>>    for achar in afile.read(maxread):
>>      if ord(achar) > 127:
>>        return 'binary'
>>    return 'text'
>> 
>> 
> 
> Pefect, thanks!

Not only is it not perfect, as given it isn't even correct.

>>> open("lots_of_nulls.bin", "w").write("\0" * 1024)
>>> test_file("lots_of_nulls.bin")
'text'

However, with a more careful algorithm for deciding what's text and what's
not, the general approach is fine.


-- 
Steven.




More information about the Python-list mailing list