Python Forum
Help extracting comment data from multiple zip files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help extracting comment data from multiple zip files (/thread-12717.html)

Pages: 1 2


RE: Help extracting comment data from multiple zip files - SoulsKeeper - Sep-10-2018

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