io.BytesIO-Inhalt in eine Datei kopieren in Python
English
Deutsch
Problem:
In Python hast du eine io.BytesIO-Instanz, die einige Daten enthält. Du möchtest diese Daten in eine Datei (oder ein anderes file-ähnliches Objekt) kopieren.
Lösung
Verwende diese Funktion:
copy_filelike.py
def copy_filelike_to_filelike(src, dst, bufsize=16384):
while True:
buf = src.read(bufsize)
if not buf:
break
dst.write(buf)Verwendungsbeispiel:
copy_bytesio_example.py
import io
myBytesIO = io.BytesIO(b"test123")
with open("myfile.txt", "wb") as outfile:
copy_filelike_to_filelike(myBytesIO, outfile)
# myfile.txt enthält nun "test123" (kein abschließender Zeilenumbruch)Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow