Python-删除目录下的空文件或者空文件夹

遍历目录下包括子文件夹等所有下属的文件以及文件夹,查找空文件以及空文件夹并删除:

def Clean_empty(path):
    """
    遍历文件下所有子文件夹以及子文件,清理空文件夹和空文件
    path:文件路径
    """
    
    for (dirpath,dirnames,filenames) in os.walk(path):
        for filename in filenames:
            file_folder=dirpath+'/'+filename
            # print(file_folder)
            if os.path.isdir(file_folder): 
                if not os.listdir(file_folder): 
                    print(file_folder)
                    # os.rmdir(dirpath+filename) 
            elif os.path.isfile(file_folder): 
                if os.path.getsize(file_folder) == 0: 
                    print(file_folder)
                    os.remove(file_folder)  
    print(path, 'clean over!')

if __name__ == "__main__": 
    path = '/data/git/ocr-platform/data/annotation_data/recognize/dataset/ocr_dataset_etc'
    Clean_empty(path)

Python-删除目录下的空文件或者空文件夹 

 

上一篇:3、进程间通信Queue和进程池Pool


下一篇:macos finder 创建smart folder