By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,472 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

Zippping a directory recursively from Python script on Windows

P: n/a
I guess that the best approach is calling a shell tool with something
like os.popen(). But I cannot seem to find any free tools.

Winzip has a command line option, but for registered users only. That is
bothersome if I want to install the script on other machines.

The same for pkwares zip. At least I cannot seem to find a free version
of it.

gzip seems able to do the trick, but I need to install Cygwin, that's
also a bother ;-)

Can it really be that there is no free .zip command line tool for
Windows, or are my Googling skills just to poor?

Ideally it should just be a single .exe file that I can put in the
folder with my script, for easy distribution.

Or is there a more Pythonic approach?
regards Max M
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
did you look at the module zipfile? This seems to be able to everything
you describe?

regards Gerrit
I guess that the best approach is calling a shell tool with something
like os.popen(). But I cannot seem to find any free tools.

Winzip has a command line option, but for registered users only. That is
bothersome if I want to install the script on other machines.

The same for pkwares zip. At least I cannot seem to find a free version
of it.

gzip seems able to do the trick, but I need to install Cygwin, that's
also a bother ;-)

Can it really be that there is no free .zip command line tool for
Windows, or are my Googling skills just to poor?

Ideally it should just be a single .exe file that I can put in the
folder with my script, for easy distribution.

Or is there a more Pythonic approach?
regards Max M

--
Gaudi systems architecting:
http://www.extra.research.philips.com/natlab/sysarch/

Jul 18 '05 #2

P: n/a
>>>>> Gerrit Muller <ge***********@embeddedsystems.nl> (GM) wrote:

GM> did you look at the module zipfile? This seems to be able to everything you
GM> describe?

And otherwise there are always the info-zip free command line utils.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@hccnet.nl
Jul 18 '05 #3

P: n/a
Gerrit Muller wrote:
did you look at the module zipfile? This seems to be able to everything
you describe?

Yes i did. But as I read it, it works on single files only.

But after your posting I found: "7.19 tarfile -- Read and write tar
archive files"

So I can just create .tar.gz files instead it seems.

That is just dandy.

Maybe that'll teach me to look better the next time.

I should have known that if it is something I need, it is in the library.
thank Max M
Jul 18 '05 #4

P: n/a
Mike C. Fletcher wrote:
Max M wrote:
but here's a simple example of using ZipFile for creating zips of
directories. You'd want to check for .jpg, .mpg, etceteras and avoid
compressing those most likely, but the basics are there.

Thanks!

Max M

Jul 18 '05 #5

P: n/a
Max M wrote:
....
Can it really be that there is no free .zip command line tool for
Windows, or are my Googling skills just to poor?
I believe InfoZip is a free command-line zip/unzip utility, but I may be
wrong about that, I normally use cygwin tar & gzip.
Or is there a more Pythonic approach?


Honestly, I wouldn't do this myself, I just tell everyone to get tar and
gzip, but here's a simple example of using ZipFile for creating zips of
directories. You'd want to check for .jpg, .mpg, etceteras and avoid
compressing those most likely, but the basics are there.

HTH,
Mike

import zipfile, os

def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for file in files:
file = os.path.join( directory, file )
# yes, the +1 is hacky...
archiveName = file[len(os.path.commonprefix( (root, file) ))+1:]
zip.write( file, archiveName, zipfile.ZIP_DEFLATED )
print file
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'v:\\temp', 'c:\\temp\\test.zip' )

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/

Jul 18 '05 #6

P: n/a
Mike C. Fletcher wrote:
Max M wrote:

Honestly, I wouldn't do this myself, I just tell everyone to get tar and
gzip, but here's a simple example of using ZipFile for creating zips of
directories. You'd want to check for .jpg, .mpg, etceteras and avoid
compressing those most likely, but the basics are there.

Ok I got around to trying out the tarfile module, and as usual it was
far easier than I would have suspected.
import tarfile

package = 'somedir'
tf = tarfile.open('%s.tar.gz' % package, 'w:gz')
tf.add(package)
tf.close()
So much for using command line tools for this... ;-)

Thanks for all the pointers.
regards Max M
Jul 18 '05 #7

P: n/a
Max,

You must use os.path.walk() along with zipfile to get
what you want. os.walk will walk the directory
tree recursively calling a function that you pass
to it. Inside of that function you can call zipfile
to add each file in that subdirectory to the .ZIP
output file.

-Larry
"Max M" <ma**@mxm.dk> wrote in message
news:40*********************@dread12.news.tele.dk. ..
I guess that the best approach is calling a shell tool with something
like os.popen(). But I cannot seem to find any free tools.

Winzip has a command line option, but for registered users only. That is
bothersome if I want to install the script on other machines.

The same for pkwares zip. At least I cannot seem to find a free version
of it.

gzip seems able to do the trick, but I need to install Cygwin, that's
also a bother ;-)

Can it really be that there is no free .zip command line tool for
Windows, or are my Googling skills just to poor?

Ideally it should just be a single .exe file that I can put in the
folder with my script, for easy distribution.

Or is there a more Pythonic approach?
regards Max M

Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.