Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Python "open" function save its content in memory or in a temp file?

Tags:

python

For the following Python code:

fp = open('output.txt', 'wb')
# Very big file, writes a lot of lines, n is a very large number
for i in range(1, n):
    fp.write('something' * n)
fp.close()

The writing process above can last more than 30 min. Sometimes I get the error MemoryError. Is the content of the file before closing stored in memory or written in a temp file? If it's in a temporary file, what is its general location on a Linux OS?

Edit:

Added fp.write in a for loop

like image 674
Thierry Lam Avatar asked Feb 10 '10 19:02

Thierry Lam


People also ask

Does Python open load file into memory?

No. As per the docs, open() wraps a system call and returns a file object, the file contents are not loaded into RAM (unless you invoke, E.G., readlines()).

How does Python save memory?

Python class objects' attributes are stored in the form of a dictionary. Thus, defining thousands of objects is the same as allocating thousands of dictionaries to the memory space. And adding __slots__ (which reduces the wastage of space and speeds up the program by allocating space for a fixed amount of attributes.)

What is in memory file in Python?

A filesystem that stored in memory. Memory filesystems are useful for caches, temporary data stores, unit testing, etc. Since all the data is in memory, they are very fast, but non-permanent. The MemoryFS constructor takes no arguments.


1 Answers

It's stored in the operating system's disk cache in memory until it is flushed to disk, either implicitly due to timing or space issues, or explicitly via fp.flush().

like image 178
Ignacio Vazquez-Abrams Avatar answered Oct 05 '22 07:10

Ignacio Vazquez-Abrams