Testing for file type

2006-05-22 Thread Andrew Robert
Hi Everyone,

Is there a way to test if a file is binary or ascii within Python?

I'd prefer not to text against file extension.

Any help you can provide would be greatly appreciated.

Thanks,
Andy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Testing for file type

2006-05-22 Thread Diez B. Roggisch
Andrew Robert wrote:

 Hi Everyone,
 
 Is there a way to test if a file is binary or ascii within Python?


try:
open(filname).read().decode(ascii)
ascii = True
except UnicodeError:
ascii = False

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Testing for file type

2006-05-22 Thread Laurent Pointal
Andrew Robert a écrit :
 Hi Everyone,
 
 Is there a way to test if a file is binary or ascii within Python?
 
 I'd prefer not to text against file extension.
 
 Any help you can provide would be greatly appreciated.

May look at unix file command sources...


A+

L.Pointal.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Testing for file type

2006-05-22 Thread BartlebyScrivener
In the Python Cookbook, 2ed, page 25, there's a script by Andrew Dalke
that uses the same heureistic criteria that Perl does and analyzes a
string and deems it binary if it contains any nulls or if more than 30%
of its characters have the high bit set or are strange control codes.

There's a follow on script that uses the same test on files.

rick

-- 
http://mail.python.org/mailman/listinfo/python-list