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
I need to extract the comment Data which you can usually see on the side after opening a Zip or a Rar file, I want to extract it into a list on a .txt file

this is what i got so far

import os

os.listdir(path=r'C:\Users\user\Desktop\archives')


from zipfile import ZipFile

zipfiles = ["unzipme.*",]
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))
but I get an Error, it has a problem with this line:
zipfiles = ["unzipme.*",]
I think I need to change unzipme.* to an actual file name.
the entire folder I am trying to do this on has 2000 files and all of them are named
unzipme

but they all have an incremented extension:
unzipme.0
unzipme.1
unzipme.2
and so on

how can I change this line
zipfiles = ["unzipme.*",]
so I can do what I need to do on the entire folder?
import os
 
from zipfile import ZipFile
 
zipfiles = os.listdir('C:\Users\user\Desktop\archives')
for zfile in zipfiles:
...
(Sep-09-2018, 10:56 AM)Axel_Erfurt Wrote: [ -> ]
import os
 
from zipfile import ZipFile
 
zipfiles = os.listdir('C:\Users\user\Desktop\archives')
for zfile in zipfiles:
...
i get this Error maybe it's because non of the files have .zip extension?
does the folder C:\Users\user\Desktop\archives exist?
(Sep-09-2018, 11:05 AM)Axel_Erfurt Wrote: [ -> ]does the folder C:\Users\user\Desktop\archives exist?

indeed
try

import os
import unicodedata
zipfiles = [unicodedata.normalize('NFC', f) for f in os.listdir(u'C:\Users\user\Desktop\archives')]
(Sep-09-2018, 11:11 AM)Axel_Erfurt Wrote: [ -> ]try

import os
import unicodedata
zipfiles = [unicodedata.normalize('NFC', f) for f in os.listdir(u'C:\Users\user\Desktop\archives')]
Don't post images, better post the output (copy in cmd and use Insert output from toolbar)

try

'C:\\Users\\user\\Desktop\\archives'
or
'C:/Users/user/Desktop/archives'
(Sep-09-2018, 12:12 PM)Axel_Erfurt Wrote: [ -> ]Don't post images, better post the output (copy in cmd and use Insert output from toolbar)

try

'C:\\Users\\user\\Desktop\\archives'
or
'C:/Users/user/Desktop/archives'

solved it by using the the other slash(/)
i get different error now
import os

import unicodedata


from zipfile import ZipFile

zipfiles = [unicodedata.normalize('NFC', f) for f in os.listdir(u'C:/Users/user/Desktop/archives')]
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))
this is the Error
C:\Users\user\Desktop\New folder>python ec3.py
Opening: unzipme.0
Traceback (most recent call last):
  File "ec3.py", line 11, in <module>
    with ZipFile(zfile, 'r') as testzip:
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 1182, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'unzipme.0'
(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.
Pages: 1 2