Search code examples
python-3.xtemporary-filesstringiobytesio

What are the differences between tempfile module and IO file-like objects


I have found that there are a lot of similarities between both modules in the area of creating temp files using io.BytesIO() or io.StringIo() and tempfile.TemporaryFile() What is the purpose of each one ?


Solution

  • io.BytesIO() create a file-like object linked to a memory area, and should be used to store binary data (like data used to represent an image, a music, a MS Word document, etc.).

    io.StringIO() create a file-like object linked to a memory area, and should be used to store text data (like a html page, a php script, etc).

    tempfile.TemporaryFile() create a temp file on the disk (not in memory). Use first argument mode to specify or not the b flag to determine if the file should store binary data or only text.