islam zidan
islam zidan

Reputation: 47

i need help to extract files from a zip file using python

i have created a zip file called shoppy and put "cats.txt" in it and now i want to extract it but my code doesn't work it gives me this error

AttributeError: '_io.TextIOWrapper' object has no attribute 'extract' 

here is my code

from zipfile import *

z=open("shoppy.zip","U")
z.extract("cats.txt")

Upvotes: 0

Views: 672

Answers (1)

mhawke
mhawke

Reputation: 87134

The first problem is that open() refers to the builtin function, not to any function in the zipfile - there is no zipfile.open() function.

To open a zip file use the zipfile.ZipFile class:

import zipfile

z = zipfile.ZipFile('shoppy.zip')
z.extract('cats.txt')

This will unzip the file into the current directory. If you would prefer to unzip into a string you can use zipfile.read():

content = z.read('cats.txt')

Now content will contain the unzipped content of the file.

Upvotes: 1

Related Questions