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: -
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()
-
10 38233 bvdet 2,851
Expert Mod 2GB
Here's a script that I use to backup the contents of a directory and it's subdirectories. It can easily be adjusted to backup only the directory contents or files with specific extensions. The file path is saved in the archive. - import zipfile, os
-
-
def makeArchive(fileList, archive):
-
"""
-
'fileList' is a list of file names - full path each name
-
'archive' is the file name for the archive with a full path
-
"""
-
try:
-
a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
-
for f in fileList:
-
print "archiving file %s" % (f)
-
a.write(f)
-
a.close()
-
return True
-
except: return False
-
-
def dirEntries(dir_name, subdir, *args):
-
'''Return a list of file names found in directory 'dir_name'
-
If 'subdir' is True, recursively access subdirectories under 'dir_name'.
-
Additional arguments, if any, are file extensions to match filenames. Matched
-
file names are added to the list.
-
If there are no additional arguments, all files found in the directory are
-
added to the list.
-
Example usage: fileList = dirEntries(r'H:\TEMP', False, 'txt', 'py')
-
Only files with 'txt' and 'py' extensions will be added to the list.
-
Example usage: fileList = dirEntries(r'H:\TEMP', True)
-
All files and all the files in subdirectories under H:\TEMP will be added
-
to the list.
-
'''
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
if not args:
-
fileList.append(dirfile)
-
else:
-
if os.path.splitext(dirfile)[1][1:] in args:
-
fileList.append(dirfile)
-
# recursively access file names in subdirectories
-
elif os.path.isdir(dirfile) and subdir:
-
print "Accessing directory:", dirfile
-
fileList.extend(dirEntries(dirfile, subdir, *args))
-
return fileList
-
-
if __name__ == '__main__':
-
folder = r'D:\Zip_Files\611 Lenox'
-
zipname = r'D:\Zip_Files\611 Lenox\test.zip'
-
makeArchive(dirEntries(folder, True), zipname)
-
HTH
Here's a script that I use to backup the contents of a directory and it's subdirectories. It can easily be adjusted to backup only the directory contents or files with specific extensions. The file path is saved in the archive.
Thank you!! Yay. I used your script to make mine work and I'm getting a better understanding of recursive functions. That was definitely where my hangup had been. Thanks again!!
@bvdet
Thanks, this is just what i was looking for in my pys60 app :D. Thanks again for the help :)
you can also do this, which is MUCH more concise: - def recursive_zip(zipf, directory, folder=None):
-
list = os.listdir(directory)
-
-
for file in list:
-
if os.path.isfile(file):
-
zipf.write(file, folder, zipfile.ZIP_DEFLATED)
-
elif os.path.isdir(file):
-
recursive_zip(zipf, os.path.join(directory, file), file)
bvdet 2,851
Expert Mod 2GB
Nakubu,
Thank you for your contribution. Please use code tags when posting code in the future.
It's not a good idea to use list and file as names of variables. The built-in functions list() and file() will be masked until the objects are deleted.
I have a question. Is zipf an open file object? It would be helpful to others reading this thread if you would post sample code that creates the file object, calls recursive_zip(), and closes the file object.
BV - Moderator
here's a revised version: -
def recursive_zip(zipf, directory, folder=None):
-
nodes = os.listdir(directory)
-
-
for item in nodes:
-
if os.path.isfile(item):
-
zipf.write(item, folder, zipfile.ZIP_DEFLATED)
-
elif os.path.isdir(item):
-
recursive_zip(zipf, os.path.join(directory, item), item)
-
-
zipf is an opened zipfile.ZipFile instance. For example: -
zipf = zipfile.ZipFile(zip, "w", compression=zipfile.ZIP_DEFLATED )
-
path = '/Users/nakubu/some_folder'
-
recursive_zip(zipf, path) //leave the first folder as None, as path is root.
-
zipf.close()
-
I found Nakubu's function helpful, but needed to modify it in several ways. Hope this helps someone: -
def recursive_zip(zipf, directory, folder = ""):
-
for item in os.listdir(directory):
-
if os.path.isfile(os.path.join(directory, item)):
-
zipf.write(os.path.join(directory, item), folder + os.sep + item)
-
elif os.path.isdir(os.path.join(directory, item)):
-
recursive_zip(zipf, os.path.join(directory, item), folder + os.sep + item)
-
These are all good, but IMHO, os.walk rocks and does the hard stuff for you.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
6 posts
views
Thread by Tung Wai Yip |
last post: by
|
reply
views
Thread by Helmut Zeisel |
last post: by
|
1 post
views
Thread by ralobao |
last post: by
|
2 posts
views
Thread by Phil Galey |
last post: by
|
1 post
views
Thread by WolfsonNYC |
last post: by
|
1 post
views
Thread by krithika.sridhar |
last post: by
|
3 posts
views
Thread by duyanning |
last post: by
|
4 posts
views
Thread by Colin J. Williams |
last post: by
| | | | | | | | | | | |