默认情况下提取zip覆盖?

时间:2017-05-31 10:35:54

标签: python zipfile

我有以下代码:

pz = open(packagezip, 'rb')

packz = zipfile.ZipFile(pz)
for name in packz.namelist():
    packz.extract(name, package_ext_loc)

pz.close()

如果要从(packagezip)中提取的文件遇到目的地位置(package_ext_loc)中具有相同名称的文件夹,此功能是否会自动覆盖文件夹和文件,因为 IS 是我想要的,如果没有,是否有一个简单的附加脚本来强制这种情况发生。

编辑过了一些网站我相信如果目标文件夹已经存在,zipfile.extractall会覆盖。我怎么能在这个例子中使用extractall。

2 个答案:

答案 0 :(得分:4)

我已经测试了你的代码,它运行正常。这意味着每次运行代码时都会覆盖文件夹中的相同现有文件。

答案 1 :(得分:1)

我已经进行了一轮确定,如果有重复的文件夹,他们就会被删除,

packagezip = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/packages/packages.zip')
userdatapackagezip = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/packages/addondata.zip')

package_ext_loc = xbmc.translatePath('special://home/addons/')
addondata_ext_loc = xbmc.translatePath('special://home/userdata/addon_data/')
yespackage = os.path.exists(packagezip)
yesaddondata = os.path.exists(userdatapackagezip)

if yespackage:
    xbmc.executebuiltin("Notification(New Packages available, Extracting,()")
    pz = open(packagezip, 'rb')
    packz = zipfile.ZipFile(pz)
    for name in packz.namelist():
        addonpath = xbmc.translatePath('special://home/addons/' + name)
        addonpathexists = os.path.exists(addonpath)
        if addonpathexists:
            xbmc.executebuiltin("Notification(Previous version exists, removing,()")
            shutil.rmtree(addonpath, ignore_errors=False)
            xbmc.executebuiltin("Notification(Please wait, preparing extraction,()")
            time.sleep(3)
            packz.extract(name, package_ext_loc)
            xbmc.executebuiltin("Notification(Extracted, REBOOT REQUIRED,()")
        else:
            xbmc.executebuiltin("Notification(No previous version, Extracting,()")
            packz.extract(name, package_ext_loc)
            xbmc.executebuiltin("Notification(Extracted, REBOOT REQUIRED,()")
    pz.close()
    xbmc.executebuiltin("Notification(Packages Extracted, checking addondata,()")

    if yesaddondata:
        xbmc.executebuiltin("Notification(Extracting addondata, Please wait!,()")
        adz = open(userdatapackagezip, 'rb')
        paddz = zipfile.ZipFile(adz)
        for name in paddz.namelist():
            addondatapath = xbmc.translatePath('special://home/userdata/addon_data/' + name)
            addondataexists = os.path.exists(addondatapath)
            if addondataexists:
                xbmc.executebuiltin("Notification(Previous data folder esists, now removing,()")
                shutil.rmtree(addondatapath, ignore_errors=False)
                xbmc.executebuiltin("Notification(Please wait, preparing extraction,()")
                time.sleep(3)
                paddz.extract(name, addondata_ext_loc)
                xbmc.executebuiltin("Notification(Extracted, maybe needs Reeboot,()")
            else:
                xbmc.executebuiltin("Notification(No previous version, Extracting,()")
                paddz.extract(name, addondata_ext_loc)
        adz.close()
    else:
        xbmc.executebuiltin("Notification(No addondata, at this time,()")

else:
    xbmc.executebuiltin("Notification(No packages, at this time,()")
    xbmc.executebuiltin("Notification(Checking addondata, for extraction,()")
    if yesaddondata:
        xbmc.executebuiltin("Notification(Extracting addondata, Please wait!,()")
        adz = open(userdatapackagezip, 'rb')
        paddz = zipfile.ZipFile(adz)
        for name in paddz.namelist():
            addondatapath = xbmc.translatePath('special://home/userdata/addon_data/' + name)
            addondataexists = os.path.exists(addondatapath)
            if addondataexists:
                xbmc.executebuiltin("Notification(Previous data folder esists, now removing,()")
                shutil.rmtree(addondatapath, ignore_errors=False)
                xbmc.executebuiltin("Notification(Please wait, preparing extraction,()")
                time.sleep(3)
                paddz.extract(name, addondata_ext_loc)
                xbmc.executebuiltin("Notification(Extracted, maybe needs Reeboot,()")
            else:
                xbmc.executebuiltin("Notification(No previous version, Extracting,()")
                paddz.extract(name, addondata_ext_loc)
        adz.close()
    else:
        xbmc.executebuiltin("Notification(No addondata, at this time,()")

我不确定zipfile是否会自动覆盖 这个kode用于kodi维护插件