利用python编程,在多个打包压缩的文件中搜索指定字符串。有很多xml文件

利用python编程,在多个打包压缩的文件中搜索指定字符串。有很多xml文件打包成了多个压缩文件,要指定其中几个,解压后搜索指定字符串,如何用python实现

ziprar.py

__author__ = 'williezh'
#!/usr/bin/env python3

import os
import sys
import time
import shutil
import zipfile
from zipfile import ZIP_DEFLATED


#Zip文件处理类
class ZFile(object):
    def __init__(self, fname, mode='r', basedir=''):
        self.fname = fname
        self.mode = mode
        if self.mode in ('w', 'a'):
            self.zfile = zipfile.ZipFile(fname, mode, compression=ZIP_DEFLATED)
        else:
            self.zfile = zipfile.ZipFile(fname, self.mode)
        self.basedir = basedir
        if not self.basedir:
            self.basedir = os.path.dirname(fname)

    def addfile(self, path, arcname=None):
        path = path.replace('//', '/')
        if not arcname:
            if path.startswith(self.basedir):
                arcname = path[len(self.basedir):]
            else:
                arcname = ''
        self.zfile.write(path, arcname)

    def addfiles(self, paths):
        for path in paths:
            if isinstance(path, tuple):
                self.addfile(*path)
            else:
                self.addfile(path)

    def close(self):
        self.zfile.close()

    def extract_to(self, path):
        for p in self.zfile.namelist():
            self.extract(p, path)

    def extract(self, fname, path):
        if not fname.endswith('/'):
            fn = os.path.join(path, fname)
            ds = os.path.dirname(fn)
            if not os.path.exists(ds):
                os.makedirs(ds)
            with open(fn, 'wb') as f:
                f.write(self.zfile.read(fname))


#创建Zip文件
def createZip(zfile, files):
    z = ZFile(zfile, 'w')
    z.addfiles(files)
    z.close()


#解压缩Zip到指定文件夹
def extractZip(zfile, path):
    z = ZFile(zfile)
    z.extract_to(path)
    z.close()


#解压缩rar到指定文件夹
def extractRar(zfile, path):
    rar_command1 = "WinRAR.exe x -ibck %s %s" % (zfile, path)
    rar_command2 = r'"C:\WinRAR.exe" x -ibck %s %s' % (zfile, path)
    try:
        res = os.system(rar_command1)
        if res == 0:
            print("Path OK.")
    except:
        try:
            res = os.system(rar_command2)
            if res == 0:
                print("Success to unrar the file {}.".format(path))
        except:
            print('Error: can not unrar the file {}'.format(path))


# 解压多个压缩文件到一个临时文件夹
def extract_files(file_list):
    newdir = str(int(time.time()))
    for fn in file_list:
        subdir = os.path.join(newdir, fn)
        if not os.path.exists(subdir):
            os.makedirs(subdir)
        if fn.endswith('.zip'):
            extractZip(fn, subdir)
        elif fn.endswith('.rar'):
            extractRar(fn, subdir)
    return newdir


# 查找一个文件夹中的某些文件, 返回文件内容包含findstr_list中所有字符串的文件
def findstr_at(basedir, file_list, findstr_list):
    files = []
    for r, ds, fs in os.walk(basedir):
        for fn in fs:
            if fn in file_list:
                with open(os.path.join(r, fn)) as f:
                    s = f.read()
                if all(i in s for i in findstr_list):
                    files.append(os.path.join(r, fn))
    return files


if __name__ == '__main__':
    files = [i for i in sys.argv[1:] if not i.startswith('-')]
    unzipfiles = [i for i in files if i.endswith('.zip') or i.endswith('.rar')]
    xmlfiles = [i for i in files if i.endswith('.xml')]
    save_unzipdir = True if '-s' in sys.argv else False
    findstr = [i.split('=')[-1] for i in sys.argv if i.startswith('--find=')]
    findstring = ', '.join(['`{}`'.format(i) for i in findstr])
    newdir = extract_files(unzipfiles)
    result  = findstr_at(newdir, xmlfiles, findstr)
    if not result:
        msg = 'None of the file(s) contain the given string {}.'
        print(msg.format(findstring))
    else:
        msg = '{} file(s) contain the given string {}:'
        print(msg.format(len(result), findstring))
        print('\n'.join([i.replace(newdir+os.sep, '') for i in sorted(result)]))

    if not save_unzipdir:
        shutil.rmtree(newdir)

$ python3 ziprar.py aaa.zip aaa2.zip aaa3.zip aaa.xml aaa1.xml aaa2.xml --find="It was" --find="when"
None of the file(s) contain the given string `It was`, `when`.
$ python3 ziprar.py aaa.zip aaa2.zip aaa3.zip aaa.xml aaa1.xml aaa2.xml --find="It was" --find="I"
2 file(s) contain the given string `It was`, `I`:
aaa.zip/aaa2.xml
aaa2.zip/aaa2.xml
$ python3 ziprar.py aaa.zip aaa2.zip aaa3.zip aaa.xml aaa1.xml aaa2.xml --find="It was"
2 file(s) contain the given string `It was`:
aaa.zip/aaa2.xml
aaa2.zip/aaa2.xml

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-07-25
思路就是解压 然后搜索啊
慢慢来 遇到问题再问
相似回答