Loading contents behind the scenes

s0suk3 at gmail.com s0suk3 at gmail.com
Thu May 22 09:08:31 EDT 2008


Hi, I wanted to know how cautious it is to do something like:

f = file("filename", "rb")
f.read()

for a possibly huge file. When calling f.read(), and not doing
anything with the return value, what is Python doing internally? Is it
loading the content of the file into memory (regardless of whether it
is discarding it immediately)?

In my case, what I'm doing is sending the return value through a
socket:

sock.send(f.read())

Is that gonna make a difference (memory-wise)? I guess I'm just
concerned with whether I can do a file.read() for any file in the
system in an efficient and memory-kind way, and with low overhead in
general. (For one thing, I'm not loading the contents into a
variable.)

Not that I'm saying that loading a huge file into memory will horribly
crash the system, but it's good to try to program in the safest way
possibly. For example, if you try something like this in the
interpreter on a Windows machine, everything will start working
slowly, and you'll likely have to reboot the OS:

s = ((("abc" * 999999) * 999999) * 99999) * 999999



More information about the Python-list mailing list