Python application: move the specified picture to the specified directory


1. Background

A while ago, I took 200 photos, and then picked 20 photos and asked someone to refine them. After finishing the repairs, I sent them to me today. Then I will compare the effect with the original photos for acceptance.
Since the photos selected in the early stage have not been archived, that is to say, they are still in the source folder of 200 photos, how to find these 20 photos from the 200 photos?
Fortunately, the refined picture has not changed the file name, and it can be found in the source folder through the file name. So, how can we quickly find out these 20 photos?

Search one by one! No, it's too much trouble!
Can Python help? sure!

Environment description: Python 3.9.12, windows11 64-bit, the code should be common, it is some built-in library.

2. Sort out the needs

In fact, it is very simple, as it was introduced earlier: just take the file name of the retouched image to find it in the source folder, and then move the file to the specified folder.
The split steps are as follows: get the name of the refined image -> search in the source folder -> put the image found in the source file into the specified folder .

2.1 Get the name of the refined map

To obtain the names of the refined 20 pictures, you can use them to os.listdir(path)take them all out directly, and you only need to pass in the corresponding ones path, which is relatively simple.
Assuming that the path of the refined graph is D:\精修图, the code can be written as follows:

import os
# 精修图路径
trim_catalog = r'D:\精修图'
# 读取所有文件
trim_files = os.listdir(trim_catalog)
print(trim_files)

2.2 Find pictures in the source folder

How to find pictures in the source file?
One solution is to first read the names of the 200 pictures in the source folder, and then match them with the names of the refined pictures one by one, and return if they match.
This is a more conventional idea, a bit like simulating the process of searching in a folder. (The example picture is drawn by AI, not the actual operation picture)

image.png

This solution is certainly possible, but it does not feel elegant enough. Is there a better solution?
Here's my solution:
Suppose the path to the source folder is D:\源文件夹.
In fact, there is no need to search, just stitch together the paths and pass them on to the next link.

import os
# 精修图的名称
trim_file = '1.png'
# 源文件夹路径
src_catalog = r'D:\源文件夹'
# 拼接路径
src_file = os.path.join(src_catalog, trim_file)
print(src_file)

In this way, there may be a problem that the picture does not exist and an exception is thrown, but this is okay. Since it can be expected, just add a layer of error reporting (described later).

2.3 Put the picture into the specified folder

Move the picture to the target folder, this can shutil.move(old_file, new_file)be achieved by .
After understanding this library and functions, it is very simple.
Assuming that the path of the original image where the target stores the refined image is D:\源文件夹\目标图, the code is as follows:

import shutil, os
# 目标目录
dst_catalog = r'D:\源文件夹\目标图'

# 精修图的名称
trim_file = '1.png'

# 拼接路径,精修图原图新的存放路径
dst_file = os.path.join(dst_catalog, trim_file)

# 移动文件,src_file 是精修图原图在源文件夹中的存放路径,在上一小节(源文件夹查找图片)中的拼接的路径
shutil.move(src_file, dst_file)

3. Integration and testing

So far, the three pieces of small codes have been completed, and the combination is:
Note: will be trim_file = '1.png'changed totrim_file = trim_files[0]

# 获取精修图名称
import os
# 精修图路径
trim_catalog = r'D:\精修图'
# 读取所有文件
trim_files = os.listdir(trim_catalog)
print(trim_files)

# 源文件夹查找图片
import os
# 精修图的名称
trim_file = trim_files[0]
# 源文件夹路径
src_catalog = r'D:\源文件夹'
# 拼接路径
src_file = os.path.join(src_catalog, trim_file)
print(src_file)

# 将图片放到指定文件夹
import shutil, os
# 目标目录
dst_catalog = r'D:\源文件夹\目标图'

# 精修图的名称
trim_file = trim_files[0]

# 拼接路径,精修图原图新的存放路径
dst_file = os.path.join(dst_catalog, trim_file)

# 移动文件,src_file 是精修图原图在源文件夹中的存放路径,在上一小节(源文件夹查找图片)中的拼接的路径
shutil.move(src_file, dst_file)

Test the above code first. If it goes well, the above code can complete the task of moving the first picture.
To complete 20 pictures, a traversal of the refined pictures is required.
Add a for loop, and integrate the code that needs to be executed in the loop. The code in the for loop is relatively simple, just put the splicing and moving code in it, as follows:

import shutil, os

# 精修图路径
trim_catalog = r'D:\精修图'
# 读取所有精修图文件
trim_files = os.listdir(trim_catalog)
# 源文件夹路径
src_catalog = r'D:\源文件夹'
# 目标目录
dst_catalog = r'D:\源文件夹\目标图'

for trim_file in trim_files:
    # 拼接路径,精修图原图 旧 的存放路径
    src_file = os.path.join(src_catalog, trim_file)
    # 拼接路径,精修图原图 新 的存放路径
    dst_file = os.path.join(dst_catalog, trim_file)
    # 移动文件
    shutil.move(src_file, dst_file)
    

Ideally, the above code would do the job, but it's not robust enough! As mentioned earlier, there may be situations where an error may be reported such as the picture does not exist. In order to avoid this situation, it is necessary to add an try…except…error judgment.

import shutil, os

# 精修图路径
trim_catalog = r'D:\精修图'
# 读取所有精修图文件
trim_files = os.listdir(trim_catalog)
# 源文件夹路径
src_catalog = r'D:\源文件夹'
# 目标目录
dst_catalog = r'D:\源文件夹\目标图'

for trim_file in trim_files:
    # 拼接路径,精修图原图 旧 的存放路径
    src_file = os.path.join(src_catalog, trim_file)
    # 拼接路径,精修图原图 新 的存放路径
    dst_file = os.path.join(dst_catalog, trim_file)

    try:
        # 移动文件
        shutil.move(src_file, dst_file)
        print('完成 %s 文件的移动。' % trim_file)
    except:
        print('文件 %s 移动失败!!!' % trim_file)


In this way, you can see which pictures are executed successfully and which ones fail to execute, and then check whether the file does not exist and deal with the failed execution. But if you have to find them one by one, can you give me another report or something like that?

import shutil, os

# 精修图路径
trim_catalog = r'D:\精修图'
# 读取所有精修图文件
trim_files = os.listdir(trim_catalog)
# 源文件夹路径
src_catalog = r'D:\源文件夹'
# 目标目录
dst_catalog = r'D:\源文件夹\目标图'

suc_no = 0
err_no = 0
err_file_name = []

for trim_file in trim_files:
    # 拼接路径,精修图原图 旧 的存放路径
    src_file = os.path.join(src_catalog, trim_file)
    # 拼接路径,精修图原图 新 的存放路径
    dst_file = os.path.join(dst_catalog, trim_file)

    try:
        # 移动文件
        shutil.move(src_file, dst_file)
        print('完成 %s 文件的移动。' % trim_file)
        suc_no += 1
    except:
        print('文件 %s 移动失败!!!' % trim_file)
        err_no += 1
        err_file_name.append(trim_file)
        
if err_no>0:
    print('最终结果:成功移动 %s 个文件,有 %s 个文件移动失败。失败文件为 %s。' % (suc_no, err_no, '、'.join(err_file_name)))
else:
    print('最终结果:成功移动 %s 个文件,无失败任务。' % suc_no)


The code is written, let's test it:
put 9 pictures in the [source folder].

image.png


Then copy three pictures and put them in the [refinement map] (assuming it has been refined)

image.png

Run the code, the result is as follows:

image.png

Next, I changed the 7.png in the [Refinish Image] to 7-1.png, and put the three images moved to the [Target Image] back into the [Source Folder], and then tried the effect. The execution results are as follows

image.png


:

image.png

Perfect! ! !

Four. Summary

This article uses Python's os and shutil libraries, using methods such as os.listdir(), os.path.join(), shutil.move()and so on, to move the specified file from the large folder to the target folder.

  • os.listdir()the name of the file used to extract the specified path;
  • os.path.join()Used to splice paths so that files can be moved through absolute paths;
  • shutil.move()Used to move files.




- End -

Guess you like

Origin blog.csdn.net/qq_45476428/article/details/131617785