user13407175
user13407175

Reputation:

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 "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.

Upvotes: 0

Views: 624

Answers (1)

Massimo
Massimo

Reputation: 3470

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()

Upvotes: 0

Related Questions