-1

I'm trying to use tkfiledialog to select a file, and then use Zipfile to extract the contents.

from zipfile import ZipFile
from tkinter import filedialog


ZipFile.extractall(filedialog.askopenfile())

Which returns this error:

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

Googling it didn't give me a clear answer, but I tried several .zip files and got the message. Any ideas?

Dharman
  • 30,962
  • 25
  • 85
  • 135
clarktwain
  • 210
  • 1
  • 3
  • 13
  • the documentation for `extractall` says the first parameter is a file path. `askopenfile()` doesn't return a file path. – Bryan Oakley Nov 29 '17 at 21:50

2 Answers2

1

filedialog.askopenfile() returns a file object, however, ZipFile.extractall takes a string (for the path). What you want is filedialog.askopenfilename(), which just returns the absolute filepath of the selected file (which means that ZipFile can use it)

Hope this helps!

Atto Allas
  • 610
  • 5
  • 16
  • That did it! Thanks for answering what was probably a dumb question. You just put an end to a lot of aggravation. – clarktwain Nov 30 '17 at 21:52
  • @clarktwain If the answer was correct, please mark it as correct so others with the same problem can find an answer quickly. – Atto Allas Nov 30 '17 at 23:44
1

You are using the zipfile library incorrectly. Try this:

from zipfile import ZipFile
from tkinter import filedialog


zip_file = ZipFile(filedialog.askopenfilename())
zip_file.extractall()
Novel
  • 13,406
  • 2
  • 25
  • 41