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
|
Help extracting comment data from multiple zip files - SoulsKeeper - Sep-09-2018 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? RE: Help extracting comment data from multiple zip files - Axel_Erfurt - Sep-09-2018 import os from zipfile import ZipFile zipfiles = os.listdir('C:\Users\user\Desktop\archives') for zfile in zipfiles: ... RE: Help extracting comment data from multiple zip files - SoulsKeeper - Sep-09-2018 (Sep-09-2018, 10:56 AM)Axel_Erfurt Wrote:i get this Error maybe it's because non of the files have .zip extension?import os from zipfile import ZipFile zipfiles = os.listdir('C:\Users\user\Desktop\archives') for zfile in zipfiles: ... RE: Help extracting comment data from multiple zip files - Axel_Erfurt - Sep-09-2018 does the folder C:\Users\user\Desktop\archives exist? RE: Help extracting comment data from multiple zip files - SoulsKeeper - Sep-09-2018 (Sep-09-2018, 11:05 AM)Axel_Erfurt Wrote: does the folder C:\Users\user\Desktop\archives exist? indeed RE: Help extracting comment data from multiple zip files - Axel_Erfurt - Sep-09-2018 try import os import unicodedata zipfiles = [unicodedata.normalize('NFC', f) for f in os.listdir(u'C:\Users\user\Desktop\archives')] RE: Help extracting comment data from multiple zip files - SoulsKeeper - Sep-09-2018 (Sep-09-2018, 11:11 AM)Axel_Erfurt Wrote: try RE: Help extracting comment data from multiple zip files - Axel_Erfurt - Sep-09-2018 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'
RE: Help extracting comment data from multiple zip files - SoulsKeeper - Sep-09-2018 (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) 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' RE: Help extracting comment data from multiple zip files - snippsat - Sep-09-2018 (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.
|