> On 2 Mar 2022, at 13:40, Davis, Matthew via Python-ideas 
> <python-ideas@python.org> wrote:
> 
> 
> Hi,
> 
> Currently shutil.copyfileobj returns nothing.
> I would like to be able to find out how many bytes were copied.
> 
> Whilst most file-like objects have a .tell() which you could use, some don’t, 
> and .tell() is not guaranteed to measure the number of bytes, it could 
> measure with other units.

Can you use os.fstat() on one of the file objects?
Can you write a helper that gets the size from the object that do not have 
tell()?

Barry

> 
> I don’t think changing the return value from None to not-None would be 
> backwards incompatible.
>  
> We could also do the same change for all other shutil.copy* methods (except 
> copytree).
>  
> Looking at the code, this seems straightforward to implement.
>  
> Existing code:
> 
> ```
> def copyfileobj(fsrc, fdst, length=0):
>     """copy data from file-like object fsrc to file-like object fdst"""
>     # Localize variable access to minimize overhead.
>     if not length:
>         length = COPY_BUFSIZE
>     fsrc_read = fsrc.read
>     fdst_write = fdst.write
>     while True:
>         buf = fsrc_read(length)
>         if not buf:
>             break
>         fdst_write(buf)
> ```
>  
> New code:
> 
> 
> ```
> def copyfileobj(fsrc, fdst, length=0):
>     """copy data from file-like object fsrc to file-like object fdst"""
>     # Localize variable access to minimize overhead.
>     if not length:
>         length = COPY_BUFSIZE
>     fsrc_read = fsrc.read
>     fdst_write = fdst.write
>     bytes_copied = 0
>     while True:
>         buf = fsrc_read(length)
>         if not buf:
>             break
>         fdst_write(buf)
>         bytes_copied += len(buf)
>     return bytes_copied
> ```
>  
> Regards,
> Matt
>  
>  
> Matthew Davis
> Data Analytics Senior Lead
> Telstra Energy – Decision Science & AI
> E: matthew.davi...@team.telstra.com | M: 0415762868
>  
>  
>  
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/ZKVI4DE7RLYQNLV3XYUUW24FOO57WB5P/
> Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6C2D45ZBI3ZKBU322J7EC2O6RLJMM6KE/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to