使用pythom生成1txt每行是Gimage目录下文件夹的名字。读取1txt里面每行是一个文件夹的名字删除mntdisk1lhlandmarkimagenettrain目录下文件夹名字没出现过在1txt中的文件夹。
生成1.txt的代码:
import os
dir_path = 'G:/image' # 图片目录路径
with open('1.txt', 'w') as f:
for folder_name in os.listdir(dir_path):
if os.path.isdir(os.path.join(dir_path, folder_name)):
f.write(folder_name + '\n')
删除不在1.txt中出现过的文件夹的代码:
import os
dir_path = '/mnt/disk1/lh/landmark/imagenet/train' # 图片目录路径
with open('1.txt', 'r') as f:
folder_names = set(f.read().splitlines())
for folder_name in os.listdir(dir_path):
folder_path = os.path.join(dir_path, folder_name)
if os.path.isdir(folder_path) and folder_name not in folder_names:
os.rmdir(folder_path)
``
原文地址: https://www.cveoy.top/t/topic/hp51 著作权归作者所有。请勿转载和采集!