简体   繁体   English

使用python从多个zip文件中提取注释

[英]Extracting comments from multiple zip files with python

i started learning python a few months ago, and I'm trying to solve a challenge that requires me to go through many [2,000~] zip files in a folder, collect all the comments in them and find a clue.几个月前我开始学习 python,我正在尝试解决一个挑战,该挑战需要我浏览一个文件夹中的许多 [2,000~] zip 文件,收集其中的所有评论并找到线索。

The part that I'm struggling with is the extraction of the comments.我正在努力的部分是评论的提取。

I imported the zipfile module, but I'm not sure how to make it go through all the files in the folder that contain the zip files, and collect all the comments.我导入了 zipfile 模块,但我不确定如何让它遍历包含 zip 文件的文件夹中的所有文件,并收集所有评论。

I'm using pycharm, and I would't mind if the result will be in the preview area insde pycharm or exported to a new .txt file我正在使用 pycharm,我不介意结果是在预览区域 insde pycharm 中还是导出到新的 .txt 文件

can anyone help me?谁能帮我?

 for i in os.listdir(path_to_folder):
        if i.endswith('.zip'):
            << Your code here>>

Try this and let me know if any issues试试这个,让我知道是否有任何问题

For looping over files, I tend to use the glob module in python.为了循环文件,我倾向于在 python 中使用 glob 模块。 It returns a list of files that match the string you specify ( see docs ).它返回与您指定的字符串匹配的文件列表(请参阅文档)。 Then once you have the list of files, you can loop over them and run some function/code on each one in turn.然后,一旦你有了文件列表,你就可以遍历它们并依次在每个文件上运行一些函数/代码。

import glob

list_of_files = glob.glob("/path/to/directory/*.zip")

for f in list_of_files:
    ***insert code for each zip file****

可以使用 zipfile 模块中的 getinfo 函数访问单个文件中的注释,即 getinfo(file_name).comment(),如本文所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM