Python finds out the differences between various files in different folders and checks for gaps

Author : houxue   2023-03-17 09:38:37 Browse: 844
Category : Python

Abstract: This article introduces a method based on the Python language, using a large folder as a standard, to detect and correct defects...

This article introduces a method based on the Python language, using a large folder as a standard, to detect and correct defects in subfolders or files contained in another large folder, and output the results of the detection and correction.

First, let's clarify the specific requirements to be implemented in this article. There is a large folder that contains a large number of subfolders, as shown in the following figure.

Large Folder

In addition, we have another large folder, which also contains a large number of subfolders, as shown in the following figure; As can be seen from the purple boxes above and below, these are two different large folders. At the same time, we also know that the number of subfolders in these two large folders, as well as the names of each subfolder, are almost identical, but the large folder shown in the figure below lacks some subfolders compared to the above figure.

Large number of subfolders

What we want to achieve now is to use the large folder shown in the first image as the standard, check for any missing subfolders in the large folder shown in the second image, and find out the names of the missing subfolders in the second large folder, as well as the number of missing subfolders.

Once we understand the specific requirements, we can start writing code. Note that this article compares the differences between sub folders in two large folders; If you want to compare the differences between the files in two large folders and the overall idea is the same, you can also use the code provided in this article.

The specific code used in this article is shown below.

1# -*- coding: utf-8 -*-
2"""
3Created on Tue Feb 21 17:12:47 2023
4
5@author: fkxxgis
6"""
7
8import os
9
10template_folder = r"E:\02_Project\01_Chlorophyll\Fishnet\ResultFolder"
11new_folder = r"E:\02_Project\01_Chlorophyll\Fishnet\ResultFolder_AI"
12
13folder_list = os.listdir(template_folder)
14new_list = os.listdir(new_folder)
15
16num = 0
17for folder in folder_list:
18    if folder not in new_list:
19        num += 1
20        print(folder, "is not in new folder!")
21print("\n", num, " folder(S) is(are) not in new folder!", sep = "")

As you can see, the overall code is also very simple. First, template_ Folder is our standard large folder, which is the folder shown in the first image at the beginning of this article; And new_ Folder is a large folder that needs to be checked for any missing subfolders, which is the folder shown in the second image at the beginning of this article.

First, based on the os. listdir() function, we iterate through each subfolder in the standard large folder, obtain the name of each subfolder, and store it in a list; Next, we use the same method to obtain the name of the subfolder in the large folder to be checked and filled, and store it in a list.

Next, we can start comparing the number of subfolders in two large folders. First, set a variable num as the calculation variable for the difference in the number of subfolders; Subsequently, through a for loop, the names of subfolders in the standard large folder are sequentially retrieved, and a search is performed in the list of subfolder names corresponding to the large folder to be checked and filled; "If a subfolder with the current name cannot be found, it means that this subfolder is missing from the second large folder. Therefore, you need to output its name and add 1 to the variable num.

After completing the above cycle, we can obtain the name and quantity of the missing subfolders in the second large folder, that is, the large folder to be checked and filled.

Running the above code will yield the results shown below.

Results

The code is very simple, and that's the end of it; If you have other requirements, you can expand the code yourself. For example, if you want to copy the missing subfolders from a large folder to be checked and filled, you can refer to the article to copy each file to the corresponding folder in combination with the remote sensing image file name: Python. At this point, the work has been completed.

    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).