473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to Zip a Directory with Python (using zipfile)

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:
Expand|Select|Wrap|Line Numbers
  1. import zipfile, os
  2.  
  3. def main():
  4.     zip = "help3.zip"
  5.     directory = "//groupstore/workgroups/documentation/test"
  6.     toZip(directory)
  7.  
  8.  
  9. def toZip(directory):
  10.     zippedHelp = zipfile.ZipFile(zip, "w", compression=zipfile.ZIP_DEFLATED )
  11.  
  12.     list = os.listdir(directory)
  13.  
  14.     for entity in list:
  15.         each = os.path.join(directory,entity)
  16.  
  17.         if os.path.isfile(each):
  18.             print each
  19.             zippedHelp.write(each,zipfile.ZIP_DEFLATED)
  20.         else:
  21.             addFolderToZip(zippedHelp,entity)
  22.  
  23.     zippedHelp.close()
  24.  
  25. #def addFolderToZip(zippedHelp,folder):
  26.  
  27.     for file in folder:
  28.             if os.path.isfile(file):
  29.                 zippedHelp.write(file, os.path.basename(file), zipfile.ZIP_DEFLATED)
  30.             elif os.path.isdir(file):
  31.                 addFolderToZip(zippedHelp,file)
  32. main()
  33.  
Nov 3 '08 #1
10 38549
bvdet
2,851 Expert Mod 2GB
Here's another thread that may be relevant: http://bytes.com/forum/thread845051.html
Nov 4 '08 #2
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.
Expand|Select|Wrap|Line Numbers
  1. import zipfile, os
  2.  
  3. def makeArchive(fileList, archive):
  4.     """
  5.     'fileList' is a list of file names - full path each name
  6.     'archive' is the file name for the archive with a full path
  7.     """
  8.     try:
  9.         a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
  10.         for f in fileList:
  11.             print "archiving file %s" % (f)
  12.             a.write(f)
  13.         a.close()
  14.         return True
  15.     except: return False
  16.  
  17. def dirEntries(dir_name, subdir, *args):
  18.     '''Return a list of file names found in directory 'dir_name'
  19.     If 'subdir' is True, recursively access subdirectories under 'dir_name'.
  20.     Additional arguments, if any, are file extensions to match filenames. Matched
  21.         file names are added to the list.
  22.     If there are no additional arguments, all files found in the directory are
  23.         added to the list.
  24.     Example usage: fileList = dirEntries(r'H:\TEMP', False, 'txt', 'py')
  25.         Only files with 'txt' and 'py' extensions will be added to the list.
  26.     Example usage: fileList = dirEntries(r'H:\TEMP', True)
  27.         All files and all the files in subdirectories under H:\TEMP will be added
  28.         to the list.
  29.     '''
  30.     fileList = []
  31.     for file in os.listdir(dir_name):
  32.         dirfile = os.path.join(dir_name, file)
  33.         if os.path.isfile(dirfile):
  34.             if not args:
  35.                 fileList.append(dirfile)
  36.             else:
  37.                 if os.path.splitext(dirfile)[1][1:] in args:
  38.                     fileList.append(dirfile)
  39.         # recursively access file names in subdirectories
  40.         elif os.path.isdir(dirfile) and subdir:
  41.             print "Accessing directory:", dirfile
  42.             fileList.extend(dirEntries(dirfile, subdir, *args))
  43.     return fileList
  44.  
  45. if __name__ == '__main__':
  46.     folder = r'D:\Zip_Files\611 Lenox'
  47.     zipname = r'D:\Zip_Files\611 Lenox\test.zip'
  48.     makeArchive(dirEntries(folder, True), zipname)
  49.  
HTH
Nov 4 '08 #3
Here's another thread that may be relevant: http://bytes.com/forum/thread845051.html

Thanks! I appreciate the help!
Nov 4 '08 #4
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!!
Nov 4 '08 #5
@bvdet
Thanks, this is just what i was looking for in my pys60 app :D. Thanks again for the help :)
Feb 27 '09 #6
Nakubu
2
you can also do this, which is MUCH more concise:

Expand|Select|Wrap|Line Numbers
  1. def recursive_zip(zipf, directory, folder=None):
  2.     list = os.listdir(directory)
  3.  
  4.     for file in list:
  5.         if os.path.isfile(file):
  6.             zipf.write(file, folder, zipfile.ZIP_DEFLATED)
  7.         elif os.path.isdir(file):
  8.             recursive_zip(zipf, os.path.join(directory, file), file)
Mar 31 '10 #7
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
Mar 31 '10 #8
Nakubu
2
here's a revised version:

Expand|Select|Wrap|Line Numbers
  1. def recursive_zip(zipf, directory, folder=None):
  2.     nodes = os.listdir(directory)
  3.  
  4.     for item in nodes:
  5.         if os.path.isfile(item):
  6.             zipf.write(item, folder, zipfile.ZIP_DEFLATED)
  7.         elif os.path.isdir(item):
  8.             recursive_zip(zipf, os.path.join(directory, item), item)
  9.  
  10.  
zipf is an opened zipfile.ZipFile instance. For example:

Expand|Select|Wrap|Line Numbers
  1. zipf = zipfile.ZipFile(zip, "w", compression=zipfile.ZIP_DEFLATED )
  2. path = '/Users/nakubu/some_folder'
  3. recursive_zip(zipf, path) //leave the first folder as None, as path is root.
  4. zipf.close()
  5.  
Mar 31 '10 #9
I found Nakubu's function helpful, but needed to modify it in several ways. Hope this helps someone:
Expand|Select|Wrap|Line Numbers
  1. def recursive_zip(zipf, directory, folder = ""):
  2.    for item in os.listdir(directory):
  3.       if os.path.isfile(os.path.join(directory, item)):
  4.          zipf.write(os.path.join(directory, item), folder + os.sep + item)
  5.       elif os.path.isdir(os.path.join(directory, item)):
  6.          recursive_zip(zipf, os.path.join(directory, item), folder + os.sep + item)
  7.  
Oct 17 '10 #10
These are all good, but IMHO, os.walk rocks and does the hard stuff for you.
Jun 17 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Tung Wai Yip | last post by:
Can I add empty directory using zipfile? When I try to add a directory it complains that it is not a file. tung
0
by: Helmut Zeisel | last post by:
I want to build a static extension of Python using SWIG and VC++ 6.0 as described in http://www.swig.org/Doc1.3/Python.html#n8 for gcc. My file is testerl.i: ========================= %module...
1
by: ralobao | last post by:
I have this code: try: file = zipfile.ZipFile(nome_arquivo) Gauge.start() #inicia o Gauge for element in file.namelist(): try: newFile = open(diretorio + element,"wb") except: newFile =...
2
by: Phil Galey | last post by:
Using the following, you can determine the size of a file: Dim fi As New IO.FileInfo(<Path to file>) MsgBox(fi.Length) .... but what about the size of a directory? The IO.DirectoryInfo object...
1
by: WolfsonNYC | last post by:
Anyone know how to enable Directory Browsing using the Cassini web server on .Net 2.0 ? Right now it says HTTP Error 403 - Forbidden when I go to a folder on my web site. Thanks, JW
1
by: krithika.sridhar | last post by:
Hi, I'm using : python setup.py bdist_rpm to create an rpm package to distribute my python app on linux. When i install the rpm, the files are installed in...
3
by: duyanning | last post by:
I have written a pyhton script that will process data file in current working directory. My script is in an different directory to data file. When I debug this script using pdb within emacs, emacs...
4
by: Colin J. Williams | last post by:
1.I have both 2.5 and 2.6 but both appear, under Recent Projects, as pcbuild. It would be helpful if the Python Version could be indicated. 2.With 2.6, Python compiles and executes OK but...
0
by: cnivas | last post by:
Hi, I'm doing a small application in python using mac os x. Now, I want to insert an image in the web application. I have given the image path also... but it shows "?" this symbol.... If the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.