0

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")
Ravipati Praveen
  • 446
  • 1
  • 7
  • 28

1 Answers1

1

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.

mhawke
  • 84,695
  • 9
  • 117
  • 138