Extracting .zip files with Python

Marius Gedminas marius at pov.lt
Sat Mar 6 15:13:16 UTC 2010


On Fri, Mar 05, 2010 at 08:14:12PM -0800, Ray Parrish wrote:
> Here I am stuck again... 8-) Here's my code as ran in the interpreter, 
> and the errors -
> 
>  >>> ThisZip = zipfile.ZipFile("/home/ray/logs/IIS/ex100102.zip", "r")
...
>  >>> ThisZip.extract(ArchiveName,"/home/ray/logs/IIS/" + ArchiveName)
...
> AttributeError: ZipFile instance has no attribute 'extract'
> 
>  >>> FileHandle = ThisZip.open(ArchiveName,"r")
...
> AttributeError: ZipFile instance has no attribute 'open'
> 
> I think the documents actually said that .extract() was a Python 2.6 
> thing, and since I'm on Python 2.5 I guess it doesn't have that, but it 
> should have .open() as it was listed above the 2.6 version seperation in 
> the docs.

http://docs.python.org/library/zipfile.html?highlight=zipfile#zipfile.ZipFile.open
says "New in version 2.6".

> Can you please tell me what I'm doing wrong, and give me some pointers 
> on extracting .zip files with the Python zipfile class?

Use ThisZip.read(filename) to get the contents as a Python string, then
write it out to a file.

Marius Gedminas
-- 
As a rule of thumb, I reckon Python to be an order of magnitude more wasteful
of CPU cycles and memory than my favourite low-level language, C++.
                -- Thomas Guest
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20100306/295359d4/attachment.sig>


More information about the ubuntu-users mailing list