Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
StringIOバッファの内容をファイルに書き込む最良の方法は何ですか?
StringIO
私は現在次のようなことをしています:
buf = StringIO() fd = open('file.xml', 'w') # populate buf fd.write(buf.getvalue ())
しかし、その後buf.getvalue()、コンテンツのコピーを作成しますか?
buf.getvalue()
使用shutil.copyfileobj:
shutil.copyfileobj
with open('file.xml', 'w') as fd: buf.seek(0) shutil.copyfileobj(buf, fd)
またはshutil.copyfileobj(buf, fd, -1)、制限されたサイズのチャンクを使用せずにファイルオブジェクトからコピーします(制御されていないメモリ消費を回避するために使用されます)。
shutil.copyfileobj(buf, fd, -1)
タグ)