What is File-like Object?

Pete Shinners pshinners at mediaone.net
Fri Sep 1 00:37:38 EDT 2000


"jun won, Seo" <linuxqna at chollian.net>
> Hi...
> What is File-like Object?

a file-like object is any object (including the regular file object)
that has the same methods as the file object. this would typically
be the .read() and/or .write() methods.

The reason many modules mention they take a file-like object is
to give the developer more flexibility. Instead of only taking
a file object, they can take any python object with the same set
of methods.

Most of the time you will probably just use a regular file object.
but you can also use many other objects if you want. usual suspects
would be stuff like sys.stdout, stringio, and similar things
(actually, sys.stdout really is a file object)

hopefully that's not too confusing. if you're still not sure,
just use a regular file object and don't sweat it.

(not sure as to your newbie-ness, a file object is returned
from the open() command) :]








More information about the Python-list mailing list