file corruption on windows - possible bug
Jeremy Jones
zanesdad at bellsouth.net
Mon May 9 11:50:55 EDT 2005
Fredrik Lundh wrote:
>Jeremy Jones wrote:
>
>
>
>>#################################
>>file_dict = {}
>>
>>a_list = [("a", "a%s" % i) for i in range(2500)]
>>b_list = [("b", "b%s" % i) for i in range(2500)]
>>c_list = [("c", "c%s" % i) for i in range(2500)]
>>d_list = [("d", "d%s" % i) for i in range(2500)]
>>
>>
>>joined_list = a_list + b_list + c_list + d_list
>>
>>for key, value in joined_list:
>> outfile = file_dict.setdefault(key, open("%s.txt" % key, "w"))
>>
>>
>
>you do realize that this opens the file again every time, so you end up having
>4x2500 file handles pointing to 4 physical files. that's a bad idea.
>
>
That *is* a bad idea, and no, I didn't realize that would be the result.
>From the "mapping types" page:
|a.setdefault(k[, x])| |a[k]| if |k in a|, else x (also setting it) (5)
*(5)*
setdefault() is like get(), except that if k is missing, x is both
returned and inserted into the dictionary as the value of k. x
defaults to None.
I took this to mean that setdefault was a short-circuit and only created
the default object once if the dictionary didn't contain the specified
key. But I guess it's *me* who is passing setdefault a new file handle a
few thousand times :-) Ouch.
>if you replace
>
> outfile = file_dict.setdefault(key, open("%s.txt" % key, "w"))
>
>with
>
> outfile = file_dict.get(key)
> if outfile is None:
> file_dict[key] = outfile = open("%s.txt" % key, "w")
>
>or, if you prefer,
>
> try:
> outfile = file_dict[key]
> except KeyError:
> file_dict[key] = outfile = open("%s.txt" % key, "w")
>
>your code won't depend on any undefined behaviour, and will work properly
>on all platforms.
>
></F>
>
>
>
>
>
Thanks (and thanks to you, Duncan, for your reply as well),
Jeremy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050509/6a8bbf27/attachment.html>
More information about the Python-list
mailing list