Learning record 03: How to use python to copy an image with a specific character name to another folder


foreword

Although sometimes it is very convenient to manually copy pictures to another folder, sometimes it is necessary to use code for unified batch processing when making deep learning datasets. Today I will share a simple small program for copying pictures written by myself.

1. Steps to use

1. Import library

code show as below:

import os			#用于对文件惊醒操作
import shutil		#在这里只用到了将图像从源路径复制到指定路径

2. Code

code show as below:

import os
import shutil


def read_name():
    source_file_path0 = 'E:\\数据集\\guofeng\\'				#注意opencv库不能打开中文路径
    name0 = os.listdir(source_file_path0)					#读取源路径下所有文件的名称,将其放在一个列表内返回,每个元素代表一个文件名
    new_file_path0 = 'E:\\数据集\\guofeng1\\'
    return name0, new_file_path0, source_file_path0


if __name__ == "__main__":
    name, new_file_path, source_file_path = read_name()
    print(name)
    for i in name:
        if 'b' in i and '45d' in i:			#对特定字符进行判断,若是有这几个字符,就复制过去
            middle_file_name = source_file_path + i			#指定文件的路径为路径名加文件名
            shutil.copy(middle_file_name, new_file_path)	#左边是源文件的路径加文件名称,后面是目标路径,可不加文件名称


Summarize

The above is what I want to talk about today. This article only briefly introduces the use of this applet for copying or extracting specific file names. It is quite convenient for those in need! You can even write a small software with pyqt5 yourself!

Guess you like

Origin blog.csdn.net/qq_43180908/article/details/116462117