在 Python 中,如何删除不包含特定文件扩展名(即 .pdf)的子目录?

In Python, how do I remove subdirectories that do NOT contain a certain file extension (ie .pdf)?

我正在尝试编写一个程序,以物理方式删除目录中没有任何 .pdf 文件的所有子目录。

我认为解决方案需要 os.walk 但还没有想出太多。

我创建了一个递归函数来执行此操作,但我不确定它是否有效

def isDirectory(path):
    if os.path.isdir(path):
        for file in listdir(path):
            newpath=os.path.join(path,file)
            isDirectory(newpath)
        if listdir(path)== []:
            os.rmdir(path)
            return
        else:
            return
    elif os.path.basename(path)=='.pdf':
        #array.append(os.path.basename(path))
        return
    else:
        os.remove(path)
        return

下面是执行所需功能的函数。 我添加了几个 print 语句来展示它的工作原理并记录下来。

def removeNonPDFDirectories(dpath):
    '''Visit 'dpath', removing any subdirectory not containing any PDF
       file. Return True if 'dpath' is removed.
    '''
    import os
    if os.path.isdir(dpath):
        print 'Entering', dpath
        entries = [os.path.join(dpath, entry) for entry in os.listdir(dpath)]
        subdirs = filter(os.path.isdir, entries)
        print '    Subdirectories:', subdirs
        if all(map(removeNonPDFDirectories, subdirs)):
            print '    All subdirectories were removed.'
            files = filter(os.path.isfile, entries)
            pdf_files = [f for f in files if f.endswith('.pdf')]
            print '    PDF files:', pdf_files
            if not pdf_files:
                try:
                    for f in files:
                        os.unlink(f)
                        print '    Removed file', f
                    os.rmdir(dpath)
                    print '    Removed directory', dpath
                except OSError as e:
                    # An error occurred: assume directory is not empty.
                    print '    ERROR:', e
                    print '    Keeping directory', dpath
                    return False
                # Directory was removed: report to caller.
                return True
        # Directory must be kept: report to caller.
        print '    Keeping directory', dpath
        return False
    else:
        return False