Is there a Python 2/3-compatible way of checking whether an object is a file?
I need to check if an object filehandler is actually a file object. This code needs to run in both Python 2 and 3. In Python 2 I could do
isinstance(filehandler, file)
However file is not part of Python 3, so this code raises a NameError when run with Python 3.
According to this answer, in Python 3 io.IOBase should be used to check if an object is a file, but Python 2's file doesn't subclass io.IOBase, so isinstance(filehandler, io.IOBase) won't work.
I thought about doing isinstance(filehandler, (io.IOBase, file)), but that still gives a NameError when I run it with Python 3.
Is there a way to do this that is compatible with both Python 2.7 and 3?