'tkFileDialog and Zipfile error: "AttributeError: '_io.TextIOWrapper' object has no attribute 'namelist'"
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?
Solution 1:[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!
Solution 2:[2]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Atto Allas |
Solution 2 | Novel |