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).
Related Contents:
- How do I delete a file or folder in Python?
- Find all files in a directory with extension .txt in Python
- Writing a list to a file with Python, with newlines
- open() in Python does not create a file if it doesn’t exist
- Print string to text file
- Read .mat files in Python
- Automatically creating directories with file output [duplicate]
- Reading binary file and looping over each byte [duplicate]
- Does reading an entire file leave the file handle open?
- Lazy Method for Reading Big File in Python?
- How to open a file for both reading and writing?
- How to open a file using the open with statement
- Writing a Python list of lists to a csv file
- Get last n lines of a file, similar to tail
- Python Pandas: How to read only first n rows of CSV files in?
- Deleting all files in a directory with Python
- Open files in ‘rt’ and ‘wt’ modes
- ValueError : I/O operation on closed file
- How to write a multidimensional array to a text file?
- Python, Pandas : write content of DataFrame into text File
- What is a good way to handle exceptions when trying to read a file in python?
- Replace string within file contents [duplicate]
- Batch Renaming of Files in a Directory
- How to append new data onto a new line
- How to read a single character at a time from a file in Python?
- How can I delete a file or folder in Python?
- Does Python have an argc argument?
- Python3 error: initial_value must be str or None, with StringIO
- Fail to get data on using read() of StringIO in python
- How to fix ”UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x9d in position 29815: character maps to ”?
- Saving response from Requests to file
- how do I clear a stringio object?
- Size of an open file object
- open file in “w” mode: IOError: [Errno 2] No such file or directory [duplicate]
- How to make file creation an atomic operation?
- Read from a log file as it’s being written using python
- Error when creating a new text file with python?
- watchdog monitoring file for changes
- How to write to .txt files in Python 3
- What is the difference between rb and r+b modes in file objects [duplicate]
- Do I need to pass the full path of a file in another directory to open()?
- Write file with specific permissions in Python
- How to load data from a file, for a unit test, in python?
- How to load data from a file, for a unit test, in python?
- Is it better to open/close a file every time vs keeping it open until the process is finished?
- Convert percent string to float in pandas read_csv
- An integer is required? open()
- Python: How to ignore #comment lines when reading in a file
- How might I remove duplicate lines from a file?
- How to read file N lines at a time? [duplicate]