Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is exactly a file-like object in Python?

Tags:

python

In http://docs.python.org/library/json.html:

simplejson.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, use_decimal[, **kw]]]]]]]]])

Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object.

I do know what read() and write() do.

But after reading this description "read()-supporting file-like object", I find I don't know what object type supports the read() and write().

And I can't find that in the rest of documentation. Anyone could elaborate more on the statement?

Why I ask this question is for getting "simplejson.load(urllib.open(...))" done.
The return value of "urllib.open(...)" is not a valid object, so I have to tailor it for simplejson. However, it seems like that string is not read()-supporting.

like image 828
Kordan Ou Avatar asked Dec 05 '10 15:12

Kordan Ou


People also ask

Is StringIO a file-like object Python?

The StringIO module is an in-memory file-like object. This object can be used as input or output to the most function that would expect a standard file object. When the StringIO object is created it is initialized by passing a string to the constructor.

How do you define file objects?

A File object is NOT the actual file. It does not contain the data that the file holds. It is an object that holds methods that affect a particular file or directory. It works as an interface between a program and the functions of the operating system that do the actual file manipulation.

Can a file be an object?

An object file is a computer file containing object code, that is, machine code output of an assembler or compiler. The object code is usually relocatable, and not usually directly executable. There are various formats for object files, and the same machine code can be packaged in different object file formats.


2 Answers

From the glossary:

file-like object

A synonym for file object

and a file object is

file object

An object exposing a file-oriented API (with methods such as read() or write()) to an underlying resource. Depending on the way it was created, a file object can mediate access to a real on-disk file or to another type of storage or communication device (for example standard input/output, in-memory buffers, sockets, pipes, etc.). File objects are also called file-like objects or streams.

There are actually three categories of file objects: raw binary files, buffered binary files and text files. Their interfaces are defined in the io module. The canonical way to create a file object is by using the open() function.

like image 50
chepner Avatar answered Sep 24 '22 09:09

chepner


In Python, a file object is an object exposing an API having methods for performing operations typically done on files, such as read() or write().

In the question's example: simplejson.load(fp, ...), the object passed as fp is only required to have a read() method, callable in the same way as a read() on a file (i.e. accepting an optional parameter size and returning either a str or a bytes object).

This does not need to be a real file, though, as long as it has a read() method.

A file-like object is just a synonym for file-object. See Python Glossary.

like image 27
Adrian W Avatar answered Sep 22 '22 09:09

Adrian W