Python Forum

Full Version: Help extracting comment data from multiple zip files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(Sep-09-2018, 12:42 PM)snippsat Wrote: [ -> ]
(Sep-09-2018, 12:21 PM)SoulsKeeper Wrote: [ -> ]solved it by using the the other slash(/)
You had it right in first post then you forgot r.
Then C:\U will give unicodeescape error.
path = r'C:\Users\user\Desktop\archives'
# Other way also work
path = 'C:/Users/user/Desktop/archives'
Change to folder you have zip in.
import os
from zipfile import ZipFile

path = r'C:\Users\user\Desktop\archives'
zipfiles = [f for f in os.listdir(path)]
for zfile in zipfiles:
    print(f"Opening: {zfile}")
    os.chdir(path) # Change to path folder
    with ZipFile(zfile, 'r') as testzip:
        print(testzip.comment)
If not changing to folder with .zip files has to use os.path.join() to create a absolute path to .zip files.
sorry for the delay, i already solved it but now i realize that i need it for Rar files and not Zip files, what do i need to change to get the same effect?

import unicodedata

from zipfile import ZipFile
rootFolder = u"C:/Users/user/Desktop/archives/"

zipfiles = [os.path.join(rootFolder, f) for f in os.listdir(rootFolder)]
for zfile in zipfiles:
    print("Opening: {}".format(zfile))
    with ZipFile(zfile, 'r') as testzip:
        print(testzip.comment) # comment for entire zip
        l = testzip.infolist() #list all files in archive
        for finfo in l:
            # per file/directory comments
            print("{}:{}".format(finfo.filename, finfo.comment))
Pages: 1 2