4

This question has been partially answered on this post: How to create a zip archive of a directory However, I would like some clarification, and since it was not directly related to the question, I am asking here. I am very new to python and have been absorbing all manner of text on the subject and have looked as thoroughly as possible through the answers here but I am not even sure how to ask the question.

when I call the function zipdir() I have to add parameters. The 'path' parameter is easy, but I have no idea what to put as the ziph parameter? I am not even sure what it 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
Community
  • 1
  • 1
DSMTurboAWD
  • 344
  • 1
  • 3
  • 16
  • 1
    So what is your question? Please don't rely on comments in your code; be *explicit* as to what your problem is, what you expected to happen and what happens instead (a [mcve]). – Martijn Pieters Jan 02 '17 at 16:45
  • Per request, I edited the question for clarity – DSMTurboAWD Jan 02 '17 at 16:50
  • I apologize for the indents. I am still pretty new at this, I believe I was creating a zipdir function, which I called later below. Pretty new at Python, so for a more interactive function, say if I wanted to allow the user to decide the save location, but it sounds like that is not the correct way to do it – DSMTurboAWD Jan 02 '17 at 17:55

2 Answers2

1

As you said the path is easy, its the directory you want to compress into the zip file as for the ziph its the target file that will eventually contain all the files inside the provided directory path. For more clarifacation i have copied an explanation written in ipython console about the zipfile.ZipFile:

Init signature: zipfile.ZipFile(file, mode='r', compression=0, allowZip64=True) Docstring: Class with methods to open, read, write, close, list zip files.

z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True)

file: Either the path to the file, or a file-like object. If it is a path, the file will be opened and closed by ZipFile. mode: The mode can be either read 'r', write 'w', exclusive create 'x', or append 'a'. compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib), ZIP_BZIP2 (requires bz2) or ZIP_LZMA (requires lzma). allowZip64: if True ZipFile will create files with ZIP64 extensions when needed, otherwise it will raise an exception when this would be necessary.

Here I think it is more understandable.

Yousef_Shamshoum
  • 787
  • 3
  • 12
1

A simpler example could help you see what's going on.

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

The code above zips the files in the current directory and creates the zipfile Python.zip that contains the archived files.

ziph is the parameter which you pass the zipf to and write the the zipfile.

In your example, you probably want to to this:

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")
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424