【问题标题】:Unzipping an EXE file in Python gives incompatible with windows error在 Python 中解压缩 EXE 文件会导致与 Windows 错误不兼容
【发布时间】:2012-12-21 23:41:11
【问题描述】:

我正在编写一个程序,它需要从设定的位置下载其他程序。当我在 Mac OS X 上测试时,我可以毫无问题地下载和运行这些程序,但是当我在 Windows 上下载并解压缩文件时,它给了我错误:

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

然后它描述了我如何检查是否需要 x86 或 x64 版本。我已经使用 Winrar 解压缩了同一个文件,并且包含的​​程序运行顺利,所以我很确定这是我的代码。

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

您能提供的任何帮助将不胜感激。

【问题讨论】:

    标签: python windows zipfile


    【解决方案1】:

    使用

    open(destfile,"wb")
    

    以二进制模式写入文件。

    【讨论】:

    • 完美运行。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2011-03-27
    • 2021-03-07
    • 1970-01-01
    相关资源
    最近更新 更多