How to copy a file in Python with a progress bar?

前端 未结 5 1174
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 06:07

When copying large files using shutil.copy(), you get no indication of how the operation is progressing..

I have put together something that works - it

5条回答
  •  无人及你
    2020-12-05 06:31

    Two things:

    • I would make the default block size a lot larger than 512. I would start with 16384 and perhaps more.
    • For modularity, it might be better to have the copy_with_prog function not output the progress bar itself, but call a callback function so the caller can decide how to display the progress.

    Perhaps something like this:

    def copy_with_prog(src, dest, callback = None):
        while True:
            # copy loop stuff
            if callback:
                callback(pos, total)
    
    prog = ProgressBar(...)
    copy_with_prog(src, dest, lambda pos, total: prog.update(pos, total))
    

提交回复
热议问题