Check if object is file-like in Python
For 3.1+, one of the following:
isinstance(something, io.TextIOBase)
isinstance(something, io.BufferedIOBase)
isinstance(something, io.RawIOBase)
isinstance(something, io.IOBase)
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.
(However, checking 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(size)
", "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).
As others have said you should generally avoid such checks. One exception is when the object might legitimately be different types and you want different behaviour depending on the type. The EAFP method doesn't always work here as an object could look like more than one type of duck!
For example an initialiser could take a file, string or instance of its own class. You might then have code like:
class A(object):
def __init__(self, f):
if isinstance(f, A):
# Just make a copy.
elif isinstance(f, file):
# initialise from the file
else:
# treat f as a string
Using EAFP here could cause all sorts of subtle problems as each initialisation path gets partially run before throwing an exception. Essentially this construction mimics function overloading and so isn't very Pythonic, but it can be useful if used with care.
As a side note, you can't do the file check in the same way in Python 3. You'll need something like isinstance(f, io.IOBase)
instead.
It is generally not good practice to have checks like this in your code at all unless you have special requirements.
In Python the typing is dynamic, why do you feel need to check whether the object is file like, rather than just using it as if it was a file and handling the resulting error?
Any check you can do is going to happen at runtime anyway so doing something like if not hasattr(fp, 'read')
and raising some exception provides little more utility than just calling fp.read()
and handling the resulting attribute error if the method does not exist.