Testing for file type

Diez B. Roggisch deets at nospam.web.de
Mon May 22 10:33:20 EDT 2006


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



More information about the Python-list mailing list