Extracting Text Files from a Zip File

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.

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.