Python zipfile dosen't release zip file

后端 未结 1 1269
予麋鹿
予麋鹿 2021-01-19 19:44

I\'m trying to use zipfile library on windows 8.1 and python 2.7.9.

I just want to remove library.zip after zipfile.open() but os.remove() throws \"WindowsError [Err

相关标签:
1条回答
  • 2021-01-19 20:39

    I think you must close your archive before deleting it or exiting the program as it says in python documentation https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.close

    So run z1.close() and z2.close() before removing an archive

    Your code must look like this:

    import os
    import zipfile as z
    
    dirs = os.listdir('build/')
    bSystemStr = dirs[0]
    
    print("[-] Merging library.zip...")
    with z.ZipFile('build/' + bSystemStr + '/library.zip', 'a') as z1:
        with z.ZipFile('build_temp/' + bSystemStr + '/library.zip', 'r') as z2:
            for t in ((n, z2.open(n)) for n in z2.namelist()):
                try:
                    z1.writestr(t[0], t[1].read())
                except:
                    pass
    
             z2.close()
    
         z1.close()
    
    
    print("[-] Cleaning temporary files...")
    os.remove('build_temp/' + bSystemStr + '/library.zip')
    

    If I'm wrong, correct me.

    0 讨论(0)
提交回复
热议问题