Python Forum
Help extracting comment data from multiple zip files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help extracting comment data from multiple zip files
#1
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?
Reply
#2
import os
 
from zipfile import ZipFile
 
zipfiles = os.listdir('C:\Users\user\Desktop\archives')
for zfile in zipfiles:
...
Reply
#3
(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?

Attached Files

Thumbnail(s)
   
Reply
#4
does the folder C:\Users\user\Desktop\archives exist?
Reply
#5
(Sep-09-2018, 11:05 AM)Axel_Erfurt Wrote: does the folder C:\Users\user\Desktop\archives exist?

indeed
Reply
#6
try

import os
import unicodedata
zipfiles = [unicodedata.normalize('NFC', f) for f in os.listdir(u'C:\Users\user\Desktop\archives')]
Reply
#7
(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')]

Attached Files

Thumbnail(s)
   
Reply
#8
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'
Reply
#9
(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'
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 224 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,073 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  python convert multiple files to multiple lists MCL169 6 1,571 Nov-25-2023, 05:31 AM
Last Post: Iqratech
Question remove all comment ? SpongeB0B 7 1,386 Oct-27-2023, 05:40 PM
Last Post: deanhystad
  splitting file into multiple files by searching for string AlphaInc 2 910 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  script to calculate data in csv-files ledgreve 0 1,113 May-19-2023, 07:24 AM
Last Post: ledgreve
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,188 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 975 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Find duplicate files in multiple directories Pavel_47 9 3,156 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  Extracting Data into Columns using pdfplumber arvin 17 5,610 Dec-17-2022, 11:59 AM
Last Post: arvin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020