473,748 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2137
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.n l>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@h ccnet.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=zip file.ZIP_DEFLAT ED
)
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.com monprefix( (root, file) ))+1:]
zip.write( file, archiveName, zipfile.ZIP_DEF LATED )
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******** *************@d read12.news.tel e.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
3101
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 colleagues noted that on her machine the launcher would complain it was missing a DLL - msvcr71.dll However, there's a very grey area concerning the redistribution of said DLL. If you've been keeping up with the dev list, and some other web
17
3875
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 XP if it makes any difference. I put a shortcut to myapp.py on the desktop and it shows up as a little green snake icon, which is really cool and Pythonic. When I double click the icon, the app launches just fine and the tkinter interface does...
3
5633
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 wScriptShellObject = win32com.client.Dispatch("WScript.Shell") shortcutName = unicode("shortcut.lnk", "utf8")
27
3979
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
5745
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 post my solution here. Is there a better one? Suppose you have a set of files in a directory c:\a\b and some additional files in c:\a\b\subdir. Using a Python script, you would
5
3294
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 must build a list of those files and process them by exectuing a system command (exec?) Can some one tell me what methods to use to: a) Walk the directory tree b) execute a system command with parameters.
34
3965
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 environment variable. Every time, I've had to go through and add this by hand, to have something resembling a usable Python installation. No such problems on Linux, whether it be Mandrake/Mandriva, Fedora Core, or Kubuntu. So why is the Windows...
3
4707
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 C:\\....\pydir, and I have set the python interpreter on the directory. When I run os.listdir(), I found my script of foo.py is right in the pydir. But, when I tried to run command-line python foo.py, to compile it, I got "Syntax error: invalid...
5
12762
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 names' with permissions for each entry such as Full Control, Modify, Read&Execute, etc. I need to (for example) periodically set Group Permissions for one group to Read, and another Group to None. I need to apply the settings to several directory...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9552
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8245
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.