Skip to content
Advertisement

What is exactly a file-like object in Python?

From the documentation for the standard library json module:

json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object) using this conversion table.

What exactly does this description mean? What object types are “.write()-supporting”, and “file-like”?

Advertisement

Answer

File-like objects are mainly StringIO objects, connected sockets and, well, actual file objects.

If everything goes well, urllib.urlopen() returns a file-like object supporting the necessary methods.

Advertisement