Minify all CSS and Javascript in a directory
<p>I am trying to make a python script that will package the contents of a directory and minify all of the JavaScript and CSS scripts.</p>

<p>If I used the code below (bottom of the post), and the directory structure inside of <code>theme_files</code> was such:</p>

<pre><code>\
 |-assets\
 |        |-css\
 |        |     |-theme.css
 |        |     |-stylesheet.css
 |        |
 |        |-js\
 |        |    |-theme.js
 |        |    |-page.js
 |
 |-index.html
</code></pre>

<p><em>(Is there a better way to do that?)</em></p>

<p>It would output the whole directory structure into the generated <code>.pak</code> file properly. However, the minified css and javascript files have no content inside of them other than their own file name.</p>

<p><strong>Example:</strong> the content of the file (supposedly minified) <code>theme.css</code> would be "theme.css"</p>

<p>That's it. Nothing else. One line.</p>

<p>Any idea what I'm doing wrong?</p>

<hr>

<pre><code>import io
import os
import zipfile
import rcssmin
import rjsmin

pakName = input("Theme Name: ").replace(" ", "_").lower()

themePak = zipfile.ZipFile(pakName +".tpk", "w")

for dirname, subdirs, files in os.walk("theme_files"):
    themePak.write(dirname)
    for filename in files:
        if not filename.endswith((".css", ".js")):
            themePak.write(os.path.join(dirname, filename))

        if filename.endswith(".css"):
            cssMinified = io.StringIO()
            cssMinified.write(rcssmin.cssmin(filename, keep_bang_comments=True))
            themePak.writestr(os.path.join(dirname, filename), cssMinified.getvalue())

        if filename.endswith(".js"):
            jsMinified = io.StringIO()
            jsMinified.write(rjsmin.jsmin(filename, keep_bang_comments=True))
            themePak.writestr(os.path.join(dirname, filename), jsMinified.getvalue())

themePak.close()
</code></pre>