The Python documentation should borrow the idea from the PHP.net

Just noting that the Python Library Reference is already over 2500 pages in PDF. This might be overwhelming even for a seasoned user.

Skimming over it, you can find plenty of usage examples that are not trivial for a casual user:

import gzip
import shutil

with open('example.txt', 'rb') as f_in:
    with gzip.open('example.txt.gz', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

…but you wouldn’t miss much because you can:

import gzip

with open('example.txt', 'rb') as f_in:
    with gzip.open('example.txt.gz', 'wb') as f_out:
        while True:
            chunk = f_in.read(1024)
            if not chunk:
                break
            f_out.write(chunk)

However, using shutil.copyfileobj is a helpful example. These examples promote the use of standard libraries well. Note that shutil.copyfileobj lacks an example in its module documentation.

1 Like