[Python] file reading, filtering and moving operations

File reading and moving

file read

  • glob.glob()function
# glob.glob获取file_a文件夹下 所有'.xml'文件 此时得到是文件的完整路径
file_a_List = glob.glob(os.path.join(file_a, '*.xml'))
# 遍历所有文件,获取文件名称(包括后缀)
for item in file_a_List:
	file_a_info.append(os.path.basename(item))
  • os.listdir()function
# 直接获取file_a文件夹下左右文件名
file_a_info_new = os.listdir(file_a)

 

file move

  • shutil.copyCopy the files to the new folder:
    • file_b: folder path
    • new_file_b: new folder path
    • name_b:file name
shutil.copy(file_b + name_b, new_file_b + name_b)
  • shutil.moveMove (cut) files to a new folder:
shutil.move(file_b + name_b, new_file_b + name_b)

 

filter files

Prefix Suffix Filter

  • .startswith(str): Determine whether the prefix of the string includes str
  • .endswith(str): Determine whether the suffix of the string includes str
pathDir = os.listdir(fileDir)  # 获取文件夹下的所有文件名
str = 'demo'
new_pathDir1 = []
new_pathDir2 = []
for i in pathDir:
	if i.startswith(str):
		new_pathDir1.append(i)  # 文件名前缀含有demo的加入new_pathDir1
	if i.endswith(str):
		new_pathDir2.append(i)  # 文件名后缀含有demo的加入new_pathDir2

 

file size filter

  • Image size filter
pathDir = os.listdir(fileDir)  # 取文件夹下的所有图片名
new_pathDir = []
min_size = 500  # 筛选图片的最小size
for i in pathDir:
    # 依次读取图片
    img = cv2.imread(fileDir + i)
    # 选择size大于阈值的图片
    if img.shape[0] > min_size and img.shape[1] > min_size:
        new_pathDir.append(i)  # 满足条件的加入new_pathDir

 

Filename comparison and moving files

  • Find the same named files in folder A and folder B
  • Then move to a new folder
import glob
import os
import shutil


def main(file_a, file_b, new_file_a, new_file_b=None):
    # 定义要处理的第一个文件夹变量
    file_a_info = []  # 文件夹里的文件,包括文件后缀格式;
    file_a_name = []  # 文件夹里面的文件名称,不包括文件后缀格式
    # 通过glob.glob来获取第一个文件夹下,所有'.xml'文件
    file_a_List = glob.glob(os.path.join(file_a, '*.xml'))
    # 遍历所有文件,获取文件名称(包括后缀)
    for item in file_a_List:
        file_a_info.append(os.path.basename(item))
    # 若文件中全是xml文件,则可以直接使用下面这句
    # file_a_info_new = os.listdir(file_a)
    
    # 遍历文件名称,去除后缀,只保留名称
    for item in file_a_info:
        (temp1, temp2) = os.path.splitext(item)
        file_a_name.append(temp1)

    # 对于第二个文件夹路径,做同样的操作
    file_b_info = []
    file_b_name = []
    file_b_List = glob.glob(os.path.join(file_b, '*.jpg'))
    for item in file_b_List:
        file_b_info.append(os.path.basename(item))
    for item in file_b_info:
        (temp1, temp2) = os.path.splitext(item)
        file_b_name.append(temp1)

    # 开始遍历
    for item_a in file_a_name:
        for item_b in file_b_name:
            # 两个文件夹中的文件名相同时,才开始移动文件
            if item_a == item_b:
                # 获取文件名(包括扩展名)
                name_a = file_a_info[file_a_name.index(item_a)]
                # 移动文件
                shutil.move(file_a + name_a, new_file_a + name_a)
                if new_file_b:  # 需要同时保存到两个文件夹时才运行
                    name_b = file_b_info[file_b_name.index(item_b)]
                    shutil.move(file_b + name_b, new_file_b + name_b)
                break
    print('!Move-Done!')


if __name__ == '__main__':
    # 要进行对比的两个文件夹路径
    file_a = 'file_a/'
    file_b = 'file_b/'

    # 要保存的文件夹路径
    new_file_a = 'new_file_a/'
    new_file_b = 'new_file_b/'

    # 递归删除之前存放帧图片的文件夹,并新建一个
    try:
        shutil.rmtree(new_file_a)
        shutil.rmtree(new_file_b)
    except OSError:
        pass
    os.mkdir(new_file_a)
    os.mkdir(new_file_b)

    main(file_a, file_b, new_file_a, new_file_b)

Guess you like

Origin blog.csdn.net/weixin_43799388/article/details/124026205
Recommended