file corruption on windows - possible bug
Bengt Richter
bokr at oz.net
Mon May 9 20:26:53 EDT 2005
On Mon, 09 May 2005 10:54:22 -0400, Jeremy Jones <zanesdad at bellsouth.net> wrote:
>I've written a piece of code that iterates through a list of items and
>determines the filename to write some piece of data to based on
>something in the item itself. Here is a small example piece of code to
>show the type of thing I'm doing::
>
>#################################
>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 are opening files multiply, since the open is a default value expression that is
always evaluated. Try replacing the above line with the following two lines:
try: outfile = file_dict[key]
except KeyError: outfile = file_dict[key] = open("%s.txt" % key, 'w')
> outfile.write("%s\n" % value)
>
>for f in file_dict.values():
> f.close()
>#################################
>
>Problem is, when I run this on Windows, I get 14,520 null ("\x00")
>characters at the front of the file and each file is 16,390 bytes long.
>When I run this script on Linux, each file is 13,890 bytes and contains
>no "\x00" characters. This piece of code::
>
I don't want to think about the _exact_ explanation, but try the above (untested ;-)
and see if the symptoms change ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list