Extracting Text Files from a Zip File
Using Python, I Am Trying to Extract Text Files from a Specific Zip File, Using the Zipfile Module. When I Try to Extract All of the Text Files Using the...
Using Python, I am trying to extract text files from a specific ZIP file, using the zipfile module.
When I try to extract all of the text files using the "extractall()" function, the text files become folders when they are extracted.
Here is my code:
import zipfile
new_zip = zipfile.ZipFile("NewZip.zip", "w")
new_zip.write("Hello.txt")
new_zip.extractall()
Can anyone tell me why the "Hello.txt" file, when extracted, becomes a folder rather than a text file? Thanks in advance.
1 Answer
You have to close the new archive, to update its content:
new_zip = zipfile.ZipFile("NewZip.zip", "w")
new_zip.write("Hello.txt")
new_zip.close()
new_zip = zipfile.ZipFile("NewZip.zip", "r")
new_zip.extractall()