For Windows, you need to open the file in binary mode since Windows distinguishes between binary and text files. It works on Linux as-is because Linux makes no such distinction.
Change the line
fh = open(fn, "w")
to
fh = open(fn, "wb")
You may also want to consider doing the following:
with open(fn, "wb") as fh:
fh.write(contents)
As it will handle closing the file for you in case of any type of exception.