473,398 Members | 2,165 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,398 software developers and data experts.

simple python script to zip files

T_T
Hi,

I'm just starting to learn some Python basics and are not familiar with
file handling.
Looking for a python scrip that zips files.
So aaa.xx bbb.yy ccc.xx should be zipped to aaa.zip bbb.zip ccc.zip

I haven't been able to type more than 'import gzip'..

Just a script I need for practical use (zip didn't do the job) and not
for educational purposes.

Feb 16 '08 #1
5 4815
T_T
On Sat, 16 Feb 2008 15:37:16 +0000, T_T wrote:
Hi,

I'm just starting to learn some Python basics and are not familiar with
file handling.
Looking for a python scrip that zips files. So aaa.xx bbb.yy ccc.xx
should be zipped to aaa.zip bbb.zip ccc.zip

I haven't been able to type more than 'import gzip'..

Just a script I need for practical use (zip didn't do the job) and not
for educational purposes.
btw. I need it for batch handling, so I want to place the file in a
directory, run it and zip all files in the directory.

Hope someone will be able to help me.
Feb 16 '08 #2
>I'm just starting to learn some Python basics and are not familiar with
>file handling.
Looking for a python scrip that zips files. So aaa.xx bbb.yy ccc.xx
should be zipped to aaa.zip bbb.zip ccc.zip

I haven't been able to type more than 'import gzip'..
Well, you ask for zip files, but then import gzip... ?
btw. I need it for batch handling, so I want to place the file in a
directory, run it and zip all files in the directory.
>>import os, zipfile
for fname in os.listdir('.'):
.... basename, ext = os.path.splitext(fname)
.... if ext.lower().endswith('zip'): continue
.... f = zipfile.ZipFile('%s.zip' % basename, 'w')
.... f.write(fname)
.... f.close()
.... print fname
....

seems to do the trick for me.

-tkc

Feb 16 '08 #3
T_T
>
seems to do the trick for me.

-tkc
Thanks! Works indeed. Strange thing is though, the files created are the
exact size as the original file. So it seems like it is zipping without
compression.
Feb 16 '08 #4
Thanks! Works indeed. Strange thing is though, the files created are the
exact size as the original file. So it seems like it is zipping without
compression.

The instantiation of the ZipFile object can take an optional
parameter to control the compression. The zipfile module only
supports storing (the default as you discovered) and "deflation":

f = zipfile.ZipFile(zipfilename, 'w',
compression=zipfile.ZIP_DEFLATED)

You can read more at

http://docs.python.org/lib/zipfile-objects.html

-tkc


Feb 16 '08 #5
T_T
f = zipfile.ZipFile(zipfilename, 'w',
compression=zipfile.ZIP_DEFLATED)

-tkc
Adding the compression rule works great, thanks again!
Feb 16 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Shalen chhabra | last post by:
Hey, Can anyone give me a snippet for running a python program over all the files in the directory. For ex: I have ten files in a directory and I want to run a python program against all of...
52
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
20
by: Matthew Thorley | last post by:
My friend sent me an email asking this: > I'm attemtping to decide which scripting language I should master and > was wondering if it's possible to do > these unixy awkish commands in python:...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
1
by: mattborkin | last post by:
Hey all, For the past 2 years I have been maintaining a webpage for a client. I have created and maintained it using Paraben's Web Page Wizard software. For me, the creation and maintenance...
2
by: tom.purl | last post by:
I use the pyGTD script to manage my todo lists and such. From Vim, I shell out a call to the gtd.py script, which updates my todo.txt file after update one of the related pyGTD files. Since I...
8
by: flit | last post by:
Hello All, I am trying to get information from a form and send it to a python script without success.. Here is my objective: User enters data in form --form send variables to python script...
10
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
11
by: Rikishi 42 | last post by:
Hi, I'm new to this group. I've tried finding my answer in existing messages, but no such luck. What I want to do is to compile/bundle/prepare/whatever_term a simple Python script for...
24
by: Joe Salmeri | last post by:
I just upgraded from Python 2.4.2 to Python 2.5.1 and have found some unexpected behavior that appears to be a bug in the os.stat module. My OS is Windows XP SP2 + all updates. I have several...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.