On this page
shutil.copyfileobj
Prev: shutil.copyfile | Up: shutil | Next: shutil.copymode
Rel: shutil.chown, shutil.copy, shutil.copy2, shutil.copyfile
Ref: docs.python.org — shutil.copyfileobj
Tags: #public #python3 #stdlib #shutil
Import
import shutil
from shutil import copyfileobj
Signature & Overview
shutil.copyfileobj(fsrc, fdst, length=0)
copy data from file-like object fsrc to file-like object fdst
Usage
import io
import shutil
src = io.BytesIO(b"streaming payload")
dst = io.BytesIO()
shutil.copyfileobj(src, dst, length=1024)
assert dst.getvalue() == b"streaming payload"
Gotchas
- Copies from the current file position: If
srcwas just written to, callsrc.seek(0)first or nothing will be copied. - Memory-safe for huge files: Streams in chunks (
length=) instead of loading the whole file into RAM.
REPL Inspect
from shutil import copyfileobj
help(copyfileobj)
Linked from
Related notes
- tuts/python3/stdlib_module/shutil/chown Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy2 Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copyfile Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copymode Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shutil/copystat shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copytree shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/disk_usage shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/Error shared tags: #shutil, #stdlib