<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Create .ZIPs from multiple folders</title>
	<atom:link href="http://soledadpenades.com/2008/08/26/create-zips-from-multiple-folders/feed/" rel="self" type="application/rss+xml" />
	<link>http://soledadpenades.com/2008/08/26/create-zips-from-multiple-folders/</link>
	<description>repeat 4[fd 100 rt 90]</description>
	<lastBuildDate>Mon, 30 Jan 2012 21:18:07 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: sole</title>
		<link>http://soledadpenades.com/2008/08/26/create-zips-from-multiple-folders/#comment-50579</link>
		<dc:creator>sole</dc:creator>
		<pubDate>Wed, 27 Aug 2008 08:19:04 +0000</pubDate>
		<guid isPermaLink="false">http://soledadpenades.com/?p=740#comment-50579</guid>
		<description>Hey! Thanks for the improved script. You&#039;re right, it didn&#039;t look for anything recursive, because for this specific case I hadn&#039;t folders inside folders, only plain files.

By the way, I applied some formatting to your code :)

I didn&#039;t know about yield in Python, and I&#039;m glad to see how my supposition about imports was right. I think I&#039;m in the right way :D

Thanks again for all!</description>
		<content:encoded><![CDATA[<p>Hey! Thanks for the improved script. You&#8217;re right, it didn&#8217;t look for anything recursive, because for this specific case I hadn&#8217;t folders inside folders, only plain files.</p>
<p>By the way, I applied some formatting to your code :)</p>
<p>I didn&#8217;t know about yield in Python, and I&#8217;m glad to see how my supposition about imports was right. I think I&#8217;m in the right way :D</p>
<p>Thanks again for all!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sergi Mansilla</title>
		<link>http://soledadpenades.com/2008/08/26/create-zips-from-multiple-folders/#comment-50578</link>
		<dc:creator>Sergi Mansilla</dc:creator>
		<pubDate>Wed, 27 Aug 2008 07:50:52 +0000</pubDate>
		<guid isPermaLink="false">http://soledadpenades.com/?p=740#comment-50578</guid>
		<description>Ugh it didn&#039;t indent my code  :(</description>
		<content:encoded><![CDATA[<p>Ugh it didn&#8217;t indent my code  :(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sergi Mansilla</title>
		<link>http://soledadpenades.com/2008/08/26/create-zips-from-multiple-folders/#comment-50577</link>
		<dc:creator>Sergi Mansilla</dc:creator>
		<pubDate>Wed, 27 Aug 2008 07:50:15 +0000</pubDate>
		<guid isPermaLink="false">http://soledadpenades.com/?p=740#comment-50577</guid>
		<description>Good work and good blog!

Your script seems likely to throw an error in the following lines:

&lt;code lang=&quot;python&quot;&gt;
for item_file in os.listdir(full_path):
                output_file.write(os.path.join(full_path, item_file), item_file)
&lt;/code&gt;

given that you are handling files and folders at the same time, and as far as I can remember (not sure though), it will attempt to read the folder information as if it were a file to write its contents inside the .zip file. I think you should filter the folders out as you did already some lines before.

Here is my attempt at writing a recursive solution for the problem:

&lt;code lang=&quot;python&quot;&gt;
from os import listdir, walk, path
from os.path import join, isdir, exists
import sys
import zipfile

if len(sys.argv) &gt; 1:
        folder = sys.argv[1]
else:
        folder = &#039;.&#039;

def dirwalk(dir):
    for root, dirs, files in walk(dir):
        for name in dirs:
            yield [root, name]

for ff, item in dirwalk(folder):
    folder_path = join(ff, item)

    dst_item = folder_path + &#039;.zip&#039;

    if not exists(dst_item):
        output_file = zipfile.ZipFile(dst_item, &quot;w&quot;, zipfile.ZIP_DEFLATED)
        file_path = join(folder_path,file)
        for item_file in [file for file in listdir(folder_path) if not isdir(file_path)]:
            output_file.write(file_path, item_file)
        output_file.close()
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Good work and good blog!</p>
<p>Your script seems likely to throw an error in the following lines:</p>
<div class="syhi_block"><code><span style="color: #ff7700;font-weight:bold;">for</span> item_file <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">&#40;</span>full_path<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output_file.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>full_path, item_file<span style="color: black;">&#41;</span>, item_file<span style="color: black;">&#41;</span></code></div>
<p>given that you are handling files and folders at the same time, and as far as I can remember (not sure though), it will attempt to read the folder information as if it were a file to write its contents inside the .zip file. I think you should filter the folders out as you did already some lines before.</p>
<p>Here is my attempt at writing a recursive solution for the problem:</p>
<div class="syhi_block"><code><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">os</span> <span style="color: #ff7700;font-weight:bold;">import</span> listdir, walk, path<br />
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span> <span style="color: #ff7700;font-weight:bold;">import</span> join, isdir, exists<br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">zipfile</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; folder = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
<span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; folder = <span style="color: #483d8b;">'.'</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> dirwalk<span style="color: black;">&#40;</span><span style="color: #008000;">dir</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> root, dirs, files <span style="color: #ff7700;font-weight:bold;">in</span> walk<span style="color: black;">&#40;</span><span style="color: #008000;">dir</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> name <span style="color: #ff7700;font-weight:bold;">in</span> dirs:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: black;">&#91;</span>root, name<span style="color: black;">&#93;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">for</span> ff, item <span style="color: #ff7700;font-weight:bold;">in</span> dirwalk<span style="color: black;">&#40;</span>folder<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; folder_path = join<span style="color: black;">&#40;</span>ff, item<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; dst_item = folder_path + <span style="color: #483d8b;">'.zip'</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> exists<span style="color: black;">&#40;</span>dst_item<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; output_file = <span style="color: #dc143c;">zipfile</span>.<span style="color: black;">ZipFile</span><span style="color: black;">&#40;</span>dst_item, <span style="color: #483d8b;">&quot;w&quot;</span>, <span style="color: #dc143c;">zipfile</span>.<span style="color: black;">ZIP_DEFLATED</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; file_path = join<span style="color: black;">&#40;</span>folder_path,<span style="color: #008000;">file</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> item_file <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #008000;">file</span> <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #008000;">file</span> <span style="color: #ff7700;font-weight:bold;">in</span> listdir<span style="color: black;">&#40;</span>folder_path<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> isdir<span style="color: black;">&#40;</span>file_path<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output_file.<span style="color: black;">write</span><span style="color: black;">&#40;</span>file_path, item_file<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; output_file.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></code></div>
]]></content:encoded>
	</item>
</channel>
</rss>

