tag:blogger.com,1999:blog-5236867476487043111.post7273261698522684220..comments2024-03-11T07:55:47.104-04:00Comments on Corey Goldberg: Python - Zip Directories RecursivelyCorey Goldberghttp://www.blogger.com/profile/06219872951977664560noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-5236867476487043111.post-71895068132531173602012-10-12T12:45:45.036-04:002012-10-12T12:45:45.036-04:00Zip file is being created but is empty for me. cod...Zip file is being created but is empty for me. code below<br /><br /><br />#!/usr/bin/env python<br />import os, zipfile<br /><br />def zipper(dir, zip_file):<br /> z = zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_DEFLATED)<br />def walker(zip, dir, files, root=dir):<br /> root_len = len(os.path.abspath(dir))<br /> for root, dirs, files in os.walk(dir):<br /> archive_root = os.path.abspath(root)[root_len:]<br /> for f in files:<br /> f = os.path.join(dir, f)<br /> archive_name = os.path.basename(f)<br /> zip.write( f, archive_name, zipfile.ZIP_DEFLATED)<br /> print f<br /> os.path.walk(dir, walker, z)<br /><br /> z.close()<br /> return zip_file<br /><br /><br /><br />if '__main__' == __name__:<br /># Late import, in case this project becomes a library, never to be run as main again<br /> import optparse<br /><br /># Populate our options, -h/--help is already there for you<br />usage = "usage: %prog [options]"<br />version="%prog 1.0"<br />parser = optparse.OptionParser(usage=usage, version=version)<br />parser.add_option("-d", "--dir", dest="inputDir", default="c:/test", action="store", help="sets the input directory to something other than the default (c:/test)")<br />parser.add_option("-f", "--file", dest="outputFile", default="c:/temp/test.zip", action="store", help="sets the output zip file to something other than the default (c:/temp/test.zip)")<br />parser.set_defaults()<br /><br /># Parse the arguments (defaults to parsing sys.argv)<br />(options, args) = parser.parse_args()<br /><br /># Here would be a good place to check what came in on the command line and<br /># call parser.error("Useful message") to exit if all is not well<br />if len(args) > 0 and (1 != options.inputDir or 1 != options.outputFile):<br /> parser.error("Additional arguments are not supported\nYou can only change the inputDir or outputFile using the -d and -f options.\nType zippy.py -h for help.\n")<br /><br /><br /># Do the actual work<br />zipper(options.inputDir, options.outputFile)Talnoreply@blogger.comtag:blogger.com,1999:blog-5236867476487043111.post-51300282254469082902009-07-24T00:28:34.232-04:002009-07-24T00:28:34.232-04:00Hi Corey,
Thanks for sharing the code. It is now ...Hi Corey,<br /><br />Thanks for sharing the code. It is now the backbone of a small auto backup script.milleplateauxhttps://www.blogger.com/profile/09455290959318568168noreply@blogger.comtag:blogger.com,1999:blog-5236867476487043111.post-87987673893314073052009-07-13T09:17:26.983-04:002009-07-13T09:17:26.983-04:00Rayjan,
very cool :) thanks for the additions. ...Rayjan,<br /><br />very cool :) thanks for the additions. see my updated post with some fixed code.Corey Goldberghttps://www.blogger.com/profile/06219872951977664560noreply@blogger.comtag:blogger.com,1999:blog-5236867476487043111.post-8476704661770530752009-07-13T09:17:03.014-04:002009-07-13T09:17:03.014-04:00Mark, I just updated my code with the fix!
thanks...Mark, I just updated my code with the fix!<br /><br />thanks,<br /><br />-CoreyCorey Goldberghttps://www.blogger.com/profile/06219872951977664560noreply@blogger.comtag:blogger.com,1999:blog-5236867476487043111.post-48221227036382718382009-07-10T18:01:33.743-04:002009-07-10T18:01:33.743-04:00Hi,
Maybe I was doing something wrong - but your ...Hi,<br /><br />Maybe I was doing something wrong - but your code didn't work for me.<br /><br />It wouldn't handle sub-directories below the path.<br /><br />Here is some code that seems to work for me (well I just created it now :) ).<br />Two other things I changed...<br />a) Used the more modern os.walk rather os.path.walk<br />b) removed the os.path.basename(f) call - this always gave you back what you had before the os.path.join on the previous line.<br /><br /><br /><br /><br />import os<br />import zipfile<br /><br /><br />def main():<br /> zipper('tsearch', 'c:/_temp/testzip.zip')<br /><br /><br />def zipper(dir, zip_file):<br /> zip = zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_DEFLATED)<br /> <br /> # get the length of the root dir<br /> root_len = len(os.path.abspath(dir))<br /> for root, dirs, files in os.walk(dir):<br /> # get the directories below the root<br /> archive_root = os.path.abspath(root)[root_len:]<br /> <br /> for f in files:<br /> # full path to the file<br /> fullpath = os.path.join(root, f)<br /> # archive path to the file<br /> archive_name = os.path.join(archive_root, f)<br /> print f<br /> zip.write(fullpath, archive_name, zipfile.ZIP_DEFLATED)<br /><br /> zip.close()<br /> return zip_file<br /><br /><br />if __name__ == '__main__':<br /> main()<br /><br /><br />(strange that you can't make Blogger treat that as Monospaced!Mark Mhttps://www.blogger.com/profile/09352622464096558516noreply@blogger.comtag:blogger.com,1999:blog-5236867476487043111.post-158992686944194952009-07-10T16:46:34.486-04:002009-07-10T16:46:34.486-04:00hi corey, thanks for sharing this code. i extended...hi corey, thanks for sharing this code. i extended zipper to have commandline options so it can be used for general recursive zipping. hope it helps. it's up on www.casualengineer.blogspot.com with credit given to you for inspiration<br /><br />cheers<br /><br /><br />#!/usr/bin/env python<br />import os, zipfile<br /><br />def zipper(dir, zip_file):<br /> z = zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_DEFLATED)<br /> def walker(zip, dir, files, root=dir):<br /> for f in files:<br /> f = os.path.join(dir, f)<br /> archive_name = os.path.basename(f)<br /> zip.write( f, archive_name, zipfile.ZIP_DEFLATED)<br /> print f<br /> os.path.walk(dir, walker, z)<br /> z.close()<br /> return zip_file<br /><br /><br /><br />if '__main__' == __name__:<br /> # Late import, in case this project becomes a library, never to be run as main again<br /> import optparse<br /> <br /> # Populate our options, -h/--help is already there for you<br /> usage = "usage: %prog [options]"<br /> version="%prog 1.0"<br /> parser = optparse.OptionParser(usage=usage, version=version)<br /> parser.add_option("-d", "--dir", dest="inputDir", default="c:/test", action="store", help="sets the input directory to something other than the default (c:/test)")<br /> parser.add_option("-f", "--file", dest="outputFile", default="c:/temp/test.zip", action="store", help="sets the output zip file to something other than the default (c:/temp/test.zip)")<br /> parser.set_defaults()<br /> <br /> # Parse the arguments (defaults to parsing sys.argv)<br /> (options, args) = parser.parse_args()<br /><br /> # Here would be a good place to check what came in on the command line and<br /> # call parser.error("Useful message") to exit if all is not well<br /> if len(args) > 0 and (1 != options.inputDir or 1 != options.outputFile):<br /> parser.error("Additional arguments are not supported\nYou can only change the inputDir or outputFile using the -d and -f options.\nType zippy.py -h for help.\n")<br /><br /> <br /> # Do the actual work<br /> zipper(options.inputDir, options.outputFile)Rayjan Wilsonhttps://www.blogger.com/profile/13002422148848504227noreply@blogger.com