in place input

A

andy

i'm working on an python application with intensive i/o.

file.read() and its cousins all allocate and return a new string.

I want to know if there is a way in which I can provide the space on which the
data is going to be put, in order to recycle memory, instead of allocating it
each time again and again.

thanks
 
P

Peter Otten

i'm working on an python application with intensive i/o.

file.read() and its cousins all allocate and return a new string.

I want to know if there is a way in which I can provide the space on which
the data is going to be put, in order to recycle memory, instead of
allocating it each time again and again.

The following may do:

import array
file("tmp.txt", "w").write("x" * 5)
buf = array.array("c")
buf.fromstring("-" * 10)
print buf
file("tmp.txt", "r").readinto(buf)
print buf

However, file.readinto() has a frightening docstring:

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top