pythonio

Python IO – show read speed interactively


is it possible to show reading speed interactively when reading a file?

The reason is that I would like to measure the speed when reading a file from network drive via Python and compare it against copying the file to local drive manually.

Let's say I have a simple open read function:

with open("path/to/network/drive/file", "rb") as f: # Show speed interactively when reading
    # do stuff

Thanks!


Solution

  • You can take advantage of the popular tqdm package, which displays not only a progress bar but also the speed of the progress, by creating a file-like object that wraps an actual file object with a read method that updates a tqdm object through its update method:

    import os
    from tqdm import tqdm
    
    class TQDMFileReader:
        def __init__(self, file):
            self.file = file
            self.tqdm = tqdm(total=os.stat(file.fileno()).st_size, unit='bytes')
    
        def read(self, size):
            value = self.file.read(size)
            self.tqdm.update(len(value))
            return value
    

    so that with:

    from time import sleep
    
    with open(__file__, 'rb') as file:
        reader = TQDMFileReader(file)
        while reader.read(20):
            sleep(.2)
    

    you get an output like:

    100%|████████████████████████████████████████| 446/446 [00:04<00:00, 96.37bytes/s]
    

    Demo: https://replit.com/@blhsing1/OverdueJoyousBooleanalgebra