, , , :
def is_binary(f):
return isinstance(f.read(0), bytes)
, , ( IOError), , io ABC, mode.
Python 3, / , :
def is_binary(f):
read = getattr(f, 'read', None)
if read is not None:
try:
data = read(0)
except (TypeError, ValueError):
pass
else:
return isinstance(data, bytes)
try:
f.write(b'')
except TypeError:
return False
return True
, ( , ), , , catching (, , likelier).