5

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 ?

Mohamed Samir
  • 367
  • 3
  • 9

1 Answers1

5

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.

Antwane
  • 20,760
  • 7
  • 51
  • 84
  • TemporaryFile here https://stackoverflow.com/questions/35278482/tempfile-temporaryfile-vs-stringio as you can see in the last two paragraphs of the answer he said it depends on the OS, so I think that there are something else behind creating this module – Mohamed Samir Jan 29 '20 at 13:17
  • This is because on Unix systems (macOS, Linux) you can mount `/tmp` directory in RAMdisk. This kind of config put files into RAM instead of filesystem when copied in `/tmp`. This is done at OS level and is not related to Python tools. – Antwane Jan 29 '20 at 13:23
  • after reading docs a little bit, I have found that it uses the disk storage, And I am going to discover more about its functionalities – Mohamed Samir Jan 29 '20 at 13:43