Use shutil.copyfileobj:
with open('file.xml', 'w') as fd:
buf.seek(0)
shutil.copyfileobj(buf, fd)
or shutil.copyfileobj(buf, fd, -1) to copy from a file object without using chunks of limited size (used to avoid uncontrolled memory consumption).