Using Zipfile to Create an Archive

This question partially answered this post: How to create a zip archive of the catalog However, I would like to get some clarification, and since this was not directly related to the question, I ask here. I am very new to python and absorb all the text on this subject and look as deep as possible using the answers here, but I'm not even sure how to ask a question.

when I call the zipdir () function, I need to add parameters. The path parameter is simple, but I don’t know how to set the ziph parameter ? I'm not even sure what he is looking for.

#!/usr/bin/env python
import os
import zipfile

toDirectory = ".\\Py\Backup"
fileName = int(time.time())

def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file))

if __name__ == '__main__':
    zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
    zipdir('tmp/', zipf)
    zipf.close()

def fileRename():
    os.renames('Python.zip', fileName + ".zip")

zipdir(toDirectory, ziph) #this is where I am not sure what to do with 'ziph'
fileRename() # Even thought he code create the zip, this does not work
+4
source share
2 answers

A simpler example will help you understand what is happening.

import os
import zipfile

def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file))

zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('.', zipf)
zipf.close()

zipfile Python.zip, .

ziph - , zipf to zip .

, , :

import os
import zipfile
import time
toDirectory = ".\\Py\Backup"
fileName = int(time.time())

def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file))

zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
os.renames('Python.zip', str(fileName) + ".zip")
+1

, , , zip , ziph, , . , ipython zipfile.ZipFile:

: zipfile.ZipFile(, = 'r', = 0, allowZip64 = True) : , , , , zip .

z = ZipFile (, = "r", = ZIP_STORED, allowZip64 = True)

file: , - .      , ZipFile. mode: 'r', write 'w', create 'x',      'a'. : ZIP_STORED ( ), ZIP_DEFLATED ( zlib),            ZIP_BZIP2 ( bz2) ZIP_LZMA ( lzma). allowZip64: True ZipFile ZIP64,           , ,           .

, .

+1

Source: https://habr.com/ru/post/1665530/


All Articles