Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python saving data inside Memory? (ram)

I am new to Python, but I didn't know this til yet. I have a basic program inside a for loop, that requests data from a site and saves it to a text file But when I checked inside my task manager I saw that the memory usage only increase? This might be a problem for me when running this for a long time. Is it standard for Python to do this or can you change it? Here is a what the program basically is

savefile = open("file.txt", "r+")
for i in savefile:
     #My code goes here
     savefile.write(i)
#end of loop
savefile.close()
like image 510
Uber Avatar asked Apr 27 '26 21:04

Uber


1 Answers

Python does not write to file until you call .close() or .flush() or until it hits a specified buffer size. This question might help you: How often does python flush to a file?

like image 156
Almog Avatar answered Apr 30 '26 10:04

Almog