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

时间:2015-05-13 17:33:30

标签: python python-2.7 subdirectory os.walk

我试图编写一个程序,以物理方式删除目录中没有任何.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

1 个答案:

答案 0 :(得分:0)

以下是执行所需功能的功能。 我已添加了几个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