import os import shutil def remove_dirs_except_tar(root_dir): for item in os.listdir(root_dir): item_path = os.path.join(root_dir, item) if os.path.isdir(item_path): shutil.rmtree(item_path) elif item.endswith('.tar'): continue if __name__ == "__main__": root_directory = '/media/data/CC3M/data/openimages_v7/openimages_v7_valid' remove_dirs_except_tar(root_directory)