Scott Rice
Scott Rice

Reputation: 1165

Unzipping an EXE file in Python gives incompatible with windows error

I am writing a program which requires downloading other programs from a set location. I can download and run these programs without a problem when I test on Mac OS X, but when I download and unzip the files on Windows, it gives me the error:

The version of this file is not compatible with the version of Windows you are running.

It then describes how I go about checking whether I need an x86 or x64 version. I have unzipped the same file using Winrar and the contained program runs without a hitch, so I am fairly sure it is my code.

def _unzip_(self,file,destdir):
    print "Unzipping %s to %s" % (file,destdir)
    z = zipfile.ZipFile(file)
    for f in z.namelist():
        # Zipfiles store paths internally using a forward slash. If os.sep
        # is not a forward slash, then we will compute an incorrect path.
        # Fix that by replacing all forward slashes with backslashes if
        # os.sep is a backslash
        if os.sep == "\\" and "/" in f:
            destfile = os.path.join(destdir,f.replace("/","\\"))
        else:
            destfile = os.path.join(destdir,f)
        if destfile.endswith(os.sep):
            if not os.path.exists(destfile):
                os.makedirs(destfile)
        else:
            file = open(destfile,"w")
            file.write(z.read(f))
            file.close()
    z.close()

Any help that you can give would be greatly appreciated.

Upvotes: 0

Views: 916

Answers (1)

Janne Karila
Janne Karila

Reputation: 25197

Use

open(destfile,"wb")

to write the file in binary mode.

Upvotes: 4

Related Questions