Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpooledTemporaryFile: units of maximum (in-memory) size?

The parameter max_size of tempfile.SpooledTemporaryFile() is the maximum size of the temporary file that can fit in memory (before it is spilled to disk). What are the units of this parameter (bytes? kilobytes?)? The documentation (both for Python 2.7 and Python 3.4) does not indicate this.

like image 533
Eric O Lebigot Avatar asked Apr 01 '15 14:04

Eric O Lebigot


People also ask

What does TMP mean in Python?

Tempfile is a Python module used in a situation, where we need to read multiple files, change or access the data in the file, and gives output files based on the result of processed data. Each of the output files produced during the program execution was no longer needed after the program was done.

Where is Tempfile stored python?

The directory named by the TEMP environment variable. The directory named by the TMP environment variable. A platform-specific location: On Windows, the directories C:\TEMP , C:\TMP , \TEMP , and \TMP , in that order.

How do I create a temp folder in Python?

import tempfile # create a temporary directory using the context manager with tempfile. TemporaryDirectory() as tmpdirname: print('created temporary directory', tmpdirname) # Outside the context manager, directory and contents have been removed.


1 Answers

The size is in bytes. From the SpooledTemporaryFile() source code:

def _check(self, file):
    if self._rolled: return
    max_size = self._max_size
    if max_size and file.tell() > max_size:
        self.rollover()

and file.tell() gives a position in bytes.

I'd say that any use of the term size in connection with Python file objects that is not expressed in bytes warrants an explicit mention. All other file methods that deal in terms of size always work in bytes as well.

like image 107
Martijn Pieters Avatar answered Sep 22 '22 08:09

Martijn Pieters