file corruption on windows - possible bug
Fredrik Lundh
fredrik at pythonware.com
Mon May 9 11:12:12 EDT 2005
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.
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>
More information about the Python-list
mailing list