Python 3 file objects are part of the io module , check the ABC classes in this module:
from io import IOBase if isinstance(someobj, IOBase):
Do not use type(obj) == file in Python 2; you would use isinstance(obj, file) instead. Even then you will want to test the possibilities; what ABC io allows you to do; the isinstance() function returns True for any object that implements all the methods defined by the base class.
Demo:
>>> from io import IOBase >>> fh = open('/tmp/demo', 'w') >>> isinstance(fh, IOBase) True >>> isinstance(object(), IOBase) False
Martijn pieters
source share