473,537 Members | 2,984 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Zip a Directory with Python (using zipfile)

3 New Member
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 38566
bvdet
2,851 Recognized Expert Moderator Specialist
Here's another thread that may be relevant: http://bytes.com/forum/thread845051.html
Nov 4 '08 #2
bvdet
2,851 Recognized Expert Moderator Specialist
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
Bertsche
3 New Member
Here's another thread that may be relevant: http://bytes.com/forum/thread845051.html

Thanks! I appreciate the help!
Nov 4 '08 #4
Bertsche
3 New Member
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
jbpseudo
1 New Member
@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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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
Michael Casile
1 New Member
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
15158
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
2486
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 testerl extern int hz(int i);
1
1590
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 = open(diretorio + element + '/',"w")
2
7874
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 doesn't have a Length property. In is there a way in VB.NET to determine the size of a directory without having to resort to importing the...
1
2107
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
3391
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 /usr/share/python/site-packages/..... directory by default.
3
8214
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 will change the current working directory to the directory which include the script, so my script cannot find the data file. I think this is the...
4
2609
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 various packages are not compiled, eg sqlite3. 3.Pythonw compiles OK but not sqlite3. 4.Mike Fletcher suggests an approach...
0
1610
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 application server runs then the image displays. but if we keep the web application on the net then it is highly impossible to run both the web server and...
0
7361
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7683
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7275
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7642
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3345
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.