【问题标题】:Nested if statement returns no such item嵌套 if 语句不返回此类项目
【发布时间】:2021-04-22 09:47:04
【问题描述】:

我的 scipt 正在搜索 zip 文件、解压缩并做我想做的事情。但是当我在 zip 文件中嵌套了 zip 文件时会出现问题,所以我想也许我复制了工作 if 语句,进行了一些调整,但我仍然无法让它工作。

print('Searching for ZipFiles')
for file in os.listdir(working_directory):
    zfile = file
    if zfile.endswith('.zip'):
        # Create a ZipFile Object and load sample.zip in it
        with ZipFile(zfile, 'r') as zipObj:
           # Get a list of all archived file names from the zip
           listOfFileNames = zipObj.namelist()
           # Iterate over the file names
           for fileName in listOfFileNames:
              zipObj.extract(fileName, './temp')
              maincom() #until here, my script is working, below is the new IF statement
              if fileName.endswith('.zip'):
                for file in os.listdir('.'):
                  zfile = file
                  if zfile.endswith('.zip'):
                  # Create a ZipFile Object and load sample.zip in it
                    with ZipFile(zfile, 'r') as zipObj:
                       # Get a list of all archived file names from the zip
                       listOfFileNames = zipObj.namelist()
                       # Iterate over the file names
                       for fileName in listOfFileNames:
                          zipObj.extract(fileName, '')
                          maincom()

我想要实现的是简单地解压缩当前目录中的嵌套zip文件,运行maincom(),如果可能,可能在解压缩完成后删除嵌套的zip文件

谢谢大家

【问题讨论】:

  • 首先,我认为你应该学会使用递归。
  • 好的,让我看看

标签: python path zip unzip


【解决方案1】:

试试这个:

import glob
import zipfile

while len(glob.glob(working_directory+"*zip")) != 0:
    for filename in glob.glob(working_directory+"*zip"):
        with ZipFile(filename, 'r') as zipObj:
            for fileName in zipObj.namelist():
                zipObj.extract(fileName, './temp')
                maincom()

原则上,这将循环直到您的工作目录中没有更多的 zipfile。

这意味着:如果 zipfiles 存在于迭代 1 中并且包含其他 zipfiles,则将提取这些文件。这意味着:zipfiles 将出现在迭代 2 中。并且将被提取。等等等等……

【讨论】:

    【解决方案2】:

    如果您的 zipfile 不是那么大,则无需将任何内容解压缩到磁盘,只需将内容保存在内存中,递归 zipfile 将使用 zipobj.read(filename) 返回的字节对象链接。

    import ZipFile
    from io import BytesIO
    
    def recursiveunziper(zipfile):
        with ZipFile(zipfile, 'r') as zipobj:
            for filename in zipobj.namelist():
                if filename.endswith('.zip'):
                    recursiveunziper(BytesIO(zipobj.read(filename)))
                if filename.contains('foobar')
                    maincom(zipobj.read(filename)) # sending bytes of foobar files to maincom
    
    for filename in os.listdir(working_directory):
        if filename.endswith('.zip'):
            recursiveunziper(filename)
    

    我不知道您的maincom() 是否只能在最里面的 zip 文件中运行,或者在所有 zip 文件中运行。在示例中,它在所有 zip 文件中包含“foobar”的所有文件中运行。进行调整以使其按您的方式工作。

    BytesIO 用于将字节对象转换为可搜索字节对象,就像它是文件一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      相关资源
      最近更新 更多