Next: HaxUp: Python

Files

Table of Contents

1 Working with Files

The builtin open function returns either a text stream or bytes stream depending on the mode argument. These are analogous to structures in the IO module:

import io

f_str = io.StringIO("some initial text data")
f_byt = io.BytesIO(b"some initial binary data: \x00\x01")

These objects support methods like readline(), read(n) which reads n characters (or bytes) from the stream and perhaps most importantly, they implement __next__ (ie they are iterators) which emits the stream contents line by line:

for line in f_str:
    print(line)

Unlike readlines() this does not pour the whole file contents into main memory, so we can feel a little bit more secure 🔒 reading larger files.

Most streams (those for which seekable() returns True) support random access via seek(offset) (link- changing the stream position may be from the start of stream, current position or end of stream, so note offset can also be negative), and we can get the current stream position using tell().

1.1 Asyncio

Local file IO is typically blocking, but we can use aiofiles in asyncio applications which uses a dedicated thread pool to avoid blocking IO:

async with aiofiles.open("filename", mode="r") as f:
    async for line in f:
        print(f)

The file object returned supports many of the methods available on standard streams. Note aiofiles also supports some async alternatives to functions in the stdlib os module.

Author: root

Created: 2024-03-23 Sat 11:44

Validate