File-like objects in Python PREMIUM

Series: Files
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Watch as video Python 3.10—3.14
Python Morsels
Watch as video
02:53

Let's talk about file-like objects in Python.

Python's print function accepts any file-like object

Python's built-in print function is used for writing to a file-like object.

We can call print is called, it writes to the file object it's provided. So these two print calls:

>>> with open("my_file.txt", mode="wt") as f:
...     print("This is some text", file=f)
...     print("This is more text", file=f)

Will write these two lines to our my_file.txt file:

This is some text
This is more text

You probably don't think of print as a tool for writing to a file, but that's how print thinks of itself.

The default value for the print function's file argument is sys.stdout (i.e. standard output):

>>> import sys
>>> print("Hello there", file=sys.stdout)

Standard output is an object that acts like a file, except that when we write to it it doesn't write to an actual file: it writes to the screen.

>>> import sys
>>> print("Hello there", file=sys.stdout)
Hello there

The print function will accept any file-like object that we can give to it.

A file-like object that writes to a string

Another example of a file-like object is the StringIO class in Python's io module. StringIO has a read method, and a write method, and it supports all the other bits of behavior that files support:

>>> from io import StringIO
>>> f = StringIO()
>>> f.read()
''
>>> f.write('hello')
5

But it doesn't actually write to a real file. Instead, it writes and reads data from a string:

>>> f.getvalue()
'hello'

So it's a fake file object that stores its data in a string under the hood.

Duck typing with file-like objects

If we take a StringIO object and pass it to the built-in print function we won't print to the screen, and we won't write to a file:

>>> from io import StringIO
>>> f = StringIO()
>>> print("This is some text", file=f)

Instead, we'll print some text to a string that's stored within this StringIO object:

>>> f.getvalue()
'This is some text\n'

We call this idea duck typing. Duck typing is the idea that if it looks like a duck and it quacks like a duck, then it's a duck. In Python, we don't care about whether something is a duck, we care about whether it behaves like a duck. In other words, we don't care about the type of an object, we care about whether the behavior of an object (does it act the way we expect).

So we don't care whether something is a file, just whether it acts like a file.

The file object just needs to have a write method

We could take this idea of duck typing to its extreme by implementing just the behavior that the print function accepts.

Here we have a class called Capture which has a write method:

class Capture:
    def __init__(self):
        self.data = []
    def write(self, text):
        self.data.append(text)

It turns out the only "behavior" print expects of its file argument is a write method.

So as long as there's a write method, the print function will call that write method with a string:

>>> f = Capture()
>>> print("This is some text", file=f)
>>> f.data
['This is some text', '\n']

Now, this Capture object isn't a proper file-like object because it doesn't support all the other things file-like objects are supposed to have. But it's file-like enough from the print function's perspective.

Summary

In Python, we often use the terms file-like object and file object interchangeably because we don't care whether something is a file as much as whether something acts like a file. That is, we care whether it has a read method, and has a write method, and supports all the other behavior that file objects support.

So if you're working with some code that accepts a file object, keep in mind that you could pass in a file-like object instead.

Now it's your turn! 🚀

We don't learn by reading or watching. We learn by doing. That means writing Python code.

Practice this topic by working on these related Python exercises.

Python Morsels
Watch as video
02:53
This is a free preview of a premium screencast. You have 1 preview remaining.