470,821 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

Need advice on reading contents of a file into memory

f = open(someFilePath, "rb")
content = []
for data in content.read()
content.append(data)
fullContent = "".join(content)

Is there a more efficient way of doing this? I'll be running this
operation on 10,000+ files where each file is an image file with size
50k-100k

Mar 15 '06 #1
3 1118
Em Qua, 2006-03-15 √*s 13:49 -0800, vinjvinj escreveu:
f = open(someFilePath, "rb")
content = []
for data in content.read()
content.append(data)
fullContent = "".join(content)

Is there a more efficient way of doing this? I'll be running this
operation on 10,000+ files where each file is an image file with size
50k-100k


If you really need everything in memory, why not just...

fullContent = open(someFilePath, "rb").read()

....?

Mar 15 '06 #2
"vinjvinj" wrote:
f = open(someFilePath, "rb")
content = []
for data in content.read()
content.append(data)
fullContent = "".join(content)

Is there a more efficient way of doing this?


read reads until end of file, so unless the source is something unusual,
a plain

fullContent = content.read()

should be good enough (not that it matters much; that join will be in
no-op, and the list/append overhead is marginal compared to the time
required to get the data from disk)

</F>

Mar 15 '06 #3
Thanks. read() did not work when I opened the file with:

f = open(someFilePath)

But after changing to f = open(someFilePath, "rb") the read() works
fine.

VJ

Mar 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Markus Hofmann | last post: by
20 posts views Thread by sahukar praveen | last post: by
50 posts views Thread by Michael Mair | last post: by
66 posts views Thread by genestarwing | last post: by
16 posts views Thread by Jm.GlezdeRueda | last post: by
7 posts views Thread by jvdb | last post: by
3 posts views Thread by =?Utf-8?B?SmF5IFZpbnRvbg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.