[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

simple python script to zip files

T_T

2/16/2008 3:37:00 PM

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.

26 Answers

T_T

2/16/2008 3:41:00 PM

0

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.

Tim Chase

2/16/2008 6:18:00 PM

0

>> 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



T_T

2/16/2008 7:34:00 PM

0

>
> 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.


Tim Chase

2/16/2008 7:53:00 PM

0

> 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-ob...

-tkc




T_T

2/16/2008 8:28:00 PM

0

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

Adding the compression rule works great, thanks again!

John Machin

2/16/2008 8:53:00 PM

0

On Feb 17, 6:52 am, Tim Chase <python.l...@tim.thechases.com> wrote:
> > 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-ob...

Confession: An infrequent user of the zipfile module, I've been caught
twice by this strange choice of default argument.

I wonder how many people actually need/want the stored (uncompressed )
format. The select few must surely document that their choice is
deliberate:

f = zipfile.ZipFile(
zipfilename,
mode='w',
compression=zipfile.ZIP_STORED, # no compression
# Yes the above is deliberate.
# See section n.nn of project specs.
)

Suggestion: for the benefit of folk who (unlike the OP and I) do read
the manual, it might help if the text had one paragraph per arg, and
included a warning:
"""
.... also works, and at least WinZip can read such files.

/compression/ is the ZIP compression method to use when writing the
archive, and should be ZIP_STORED or ZIP_DEFLATED. *WARNING:* the
default is ZIP_STORED (no compression). Unrecognized values will cause
RuntimeError to be raised. If ZIP_DEFLATED is specified but the zlib
module is not available, RuntimeError is also raised.

If /allowZip64/ is True ...
"""

Cheers,
John

sheldonlg

6/3/2014 10:39:00 PM

0

On 6/3/2014 6:29 PM, (PeteCresswell) wrote:
> Per Shelly:
>> Continuing at the present rate of population
>> growth, the inevitable result is that Malthus will be proven correct --
>> at least regionally if not globally.
>
> I keep reading that Erlich's "The Population Bomb" (essentially
> Malthusian) has been proven to be incorrect.
>
> Seems to me more like we got a little reprieve/delay courtesy of the
> green revolution and the bomb is still ticking away.

Exactly. I couldn't have set it better.

--
Shelly

Yisroel Markov

6/6/2014 4:24:00 PM

0

On Tue, 3 Jun 2014 17:05:02 +0000 (UTC), Shelly
<sheldonlg@thevillages.net> said:

>On 6/3/2014 12:52 PM, Yisroel Markov wrote:
>> A propos the recent discussion of an Israeli firm building
>> desalination plants in California:
>> -----------------------------------
>>
>> Water scarcity has long been a favorite topic for the Chicken Littles
>> of the world. But doomsayers who predict a coming global shortage of
>> freshwater fail to account for one of the strongest forces on our
>> planet today: human ingenuity. That innovative drive is on full
>> display in Israel. After its driest winter on record, the parched
>> Mediterranean nation isn?t worried about not meeting its water needs,
>> thanks to conservation measures and a slew of new desalination plants
>> that make seawater into fresh.
>>
>> Full article at
>> http://www.the-american-interest.com/blog/2014/06/02/water-wars...
>
>This is all well and good, but the supply of fresh water is finite as
>are the natural resources.

Huh? Water is as infinite a resource as it gets, being 100%
recyclable. Net of the volcanic additions and loss to space, the
amount of water on Earth hadn't changed in billions of years. Nearly
every single molecule of H2O, including those currently locked up in
ice sheets, has passed through numerous living organisms.

>Continuing at the present rate of population
>growth, the inevitable result is that Malthus will be proven correct --
>at least regionally if not globally.

I disagree. All predictions I'm familiar with point to a population
peak by 2100 at nine billion as the worst-case scenario. Most of that
growth is supposed to come from Africa, where it's directly contingent
on access to new resources, including fresh water. IOW, if the tech
improvements don't materialize, neither will the population.

Malthus was too down on human ingenuity. Africa is losing so much food
to outdated storage methods that simply bringing losses down to the
First World average would prevent all famine there. So would ditching
the irrational fear of GMOs that many African governments exhibit. Of
course, then the Earth might comfortably support ten billion or more
humans, worrying you even more...
--
Yisroel "Godwrestler Warriorson" Markov - Boston, MA Member
www.reason.com -- for a sober analysis of the world DNRC
--------------------------------------------------------------------
"Judge, and be prepared to be judged" -- Ayn Rand

(PeteCresswell)

6/6/2014 10:55:00 PM

0

Per Yisroel Markov:
>Huh? Water is as infinite a resource as it gets, being 100%
>recyclable.

Maybe technically. But if somebody is drinking from an aquifer that
was layed down when mammoths walked the earth and somebody else sucks it
dry using it as a source for wasteful irrigation, than that first
somebody is going to pay a *lot* more for desalinated water or water
shipped in from some faraway place.
--
Pete Cresswell

Malcolm McLean

6/8/2014 7:13:00 AM

0

On Friday, June 6, 2014 5:23:49 PM UTC+1, Yisroel Markov wrote:
>
> Malthus was too down on human ingenuity. Africa is losing so much food
> to outdated storage methods that simply bringing losses down to the
> First World average would prevent all famine there. So would ditching
> the irrational fear of GMOs that many African governments exhibit. Of
> course, then the Earth might comfortably support ten billion or more
> humans, worrying you even more...
>
Preservatives and plastic packaging have a bad name amongst people who
consider themselves "environmentally aware", but they do cut food
wastage dramatically. A lot of African governments are extremely
sensible when it comes to genetic modification. However they don't
have the resources to invest in developing GM crops for their
particular agricultural conditions. The ignorant anti-GM campaign
is led by Europeans.