I'm relatively new to python, and am trying to zip a directory containing several levels of files and folders. I can use the walk function to name each file in my directory, and I can use zipfile to zip a flat number of files in one folder, but I am having heck of a time trying to zip the whole directory. I want to zip it all to a file called "help.zip", and I want it to retain the original file structure.
Of my several tries, here is the latest:
Of my several tries, here is the latest:
Code:
import zipfile, os
def main():
zip = "help3.zip"
directory = "//groupstore/workgroups/documentation/test"
toZip(directory)
def toZip(directory):
zippedHelp = zipfile.ZipFile(zip, "w", compression=zipfile.ZIP_DEFLATED )
list = os.listdir(directory)
for entity in list:
each = os.path.join(directory,entity)
if os.path.isfile(each):
print each
zippedHelp.write(each,zipfile.ZIP_DEFLATED)
else:
addFolderToZip(zippedHelp,entity)
zippedHelp.close()
#def addFolderToZip(zippedHelp,folder):
for file in folder:
if os.path.isfile(file):
zippedHelp.write(file, os.path.basename(file), zipfile.ZIP_DEFLATED)
elif os.path.isdir(file):
addFolderToZip(zippedHelp,file)
main()
Comment