w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
urllib produces garbled output under windows?

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")


fh = open(fn, "wb")

You may also want to consider doing the following:

with open(fn, "wb") as fh:

As it will handle closing the file for you in case of any type of exception.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.