2014-04-11 80 views

回答

1
import zipfile 
import os 

dir_to_zip = "test_dir" 
zip_file_name = "myzipfile.zip" 
tempfile = "myzipfile.zip.tmp" 
X = 2 

# Create the first zipfile 
z = zipfile.ZipFile(zip_file_name, "w") 

z.write(dir_to_zip) 

for dirpath, dirnames, filenames in os.walk(dir_to_zip): 
    for filename in filenames: 
     z.write(os.path.join(dirpath, filename)) 

z.close() 

for _i in range(X): 

    # Create the new zipfile as a temp file 
    z = zipfile.ZipFile(tempfile, "w") 
    z.write(zip_file_name) 
    z.close() 

    # Now remove the old zip file, replacing it with the new 
    os.remove(zip_file_name) 
    os.rename(tempfile, zip_file_name) 

编辑:现在测试和工作。 编辑2:这将教我不要打开100个zip文件 - 更改为2以上,使健康测试更容易:-)

+0

工程!但你忘了在开始时输入'os'。谢谢 – Callum

+0

谢谢,我只是测试它,发现相同的东西!找到'故意的'错误很好! –

+0

它似乎没有保留原始文件夹的内容,并将整个目录从“C:”拉到任何地方。 – Callum