473,434 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Zippping a directory recursively from Python script on Windows

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
7 2120
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
>>>>> 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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Michael Kearns | last post by:
I've been using python to write a simple 'launcher' for one of our Java applications for quite a while now. I recently updated it to use python 2.4, and all seemed well. Today, one of my...
17
by: Paul Rubin | last post by:
Dumb question from a Windows ignoramus: I find myself needing to write a Python app (call it myapp.py) that uses tkinter, which as it happens has to be used under (ugh) Windows. That's Windows...
3
by: Stanislaw Findeisen | last post by:
Does anyone know how to create file shortcuts in Windows? The only way I know is like: --------------------------------------------------------------- import win32com.client ...
27
by: Julien Fiore | last post by:
Do you wand to install Pyrex on Windows ? Here is a step-by-step guide explaining: A) how to install Pyrex on Windows XP. B) how to compile a Pyrex module. Julien Fiore, U. of Geneva
12
by: pac | last post by:
I'm preparing to distribute a Windows XP Python program and some ancillary files, and I wanted to put everything in a .ZIP archive. It proved to be inordinately difficult and I thought I would...
5
by: KraftDiner | last post by:
Hi I need help writing a python script that traverses (recursivly) a directory and its sub directories and processes all files in the directory. So at each directory if there are files in it I...
34
by: Ben Sizer | last post by:
I've installed several different versions of Python across several different versions of MS Windows, and not a single time was the Python directory or the Scripts subdirectory added to the PATH...
3
by: cosmo_general | last post by:
Hi Folks, I just downloaded precompiled Python for Windows, and it runs. Now I have got the command line coding. However, I can't run my python scripts. My python script, foo.py, is located in...
5
by: Tim Arnold | last post by:
hi, I need to set file permissions on some directory trees in windows using Python. When I click on properties for a file and select the 'Security' tab, I see a list of known 'Group or user...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.