For 3.1+, one of the following:
For 2.x, “file-like object” is too vague a thing to check for, but the documentation for whatever function(s) you’re dealing with will hopefully tell you what they actually need; if not, read the code.
As other answers point out, the first thing to ask is what exactly you’re checking for. Usually, EAFP is sufficient, and more idiomatic.
The glossary says “file-like object” is a synonym for “file object”, which ultimately means it’s an instance of one of the three abstract base classes defined in the
io module, which are themselves all subclasses of
IOBase. So, the way to check is exactly as shown above.
IOBase isn’t very useful. Can you imagine a case where you need to distinguish an actual file-like
read(size) from some one-argument function named
read that isn’t file-like, without also needing to distinguish between text files and raw binary files? So, really, you almost always want to check, e.g., “is a text file object”, not “is a file-like object”.)
For 2.x, while the
io module has existed since 2.6+, built-in file objects are not instances of
io classes, neither are any of the file-like objects in the stdlib, and neither are most third-party file-like objects you’re likely to encounter. There was no official definition of what “file-like object” means; it’s just “something like a builtin file object“, and different functions mean different things by “like”. Such functions should document what they mean; if they don’t, you have to look at the code.
However, the most common meanings are “has
read()“, or “is an iterable of strings”, but some old libraries may expect
readline instead of one of those, some libraries like to
close() files you give them, some will expect that if
fileno is present then other functionality is available, etc. And similarly for
write(buf) (although there are a lot fewer options in that direction).