This paper describes a system based onPythonlanguage to afileAs a standard for anotherfileinclusivesubfoldermaybefile(before polysyllabic verb) handle itlit. check leaks and make up for deficiencies (idiom); fig. to study a problem to find the best solution, and the method of exporting the results of the checking.
First, let's identify the specific requirements to be realized in this paper. There is an existingfileIt contains a large number ofsubfolder, as shown in the figure below.
In addition, we have anotherfile, which also contains a large number ofsubfolder, as shown below; as you can see from the purple boxes in the upper and lower panels, these are two differentfile. But at the same time, we also know that bothfilehit the nail on the headsubfolderThe number, as well as the names of each subfolder, are almost identical - but the larger folder shown below is missing some of thesubfolder。
What we now hope to accomplish is to take the first image shown in thefileAs a standard, for the second figure shown infilehit the nail on the headsubfolderand to identify the secondfilemissingsubfolderand the name of the missingsubfolderThe number of
Knowing the specific requirements, we can start writing the code. Note here that this article compares twofilecentersubfolderdifferences; if one wishes to compare twofilecenterfileThe differences, and the overall idea is all the same, or you can use the code provided in this article.
The specific code used in this article is shown below.
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 21 17:12:47 2023
@author: fkxxgis
"""
import os
template_folder = r"E:\02_Project\01_Chlorophyll\Fishnet\ResultFolder"
new_folder = r"E:\02_Project\01_Chlorophyll\Fishnet\ResultFolder_AI"
folder_list = (template_folder)
new_list = (new_folder)
num = 0
for folder in folder_list:
if folder not in new_list:
num += 1
print(folder, "is not in new folder!")
print("\n", num, " folder(S) is(are) not in new folder!", sep = "")
As you can see, the code as a whole is also very simple. First.template_folder
It's us as the standardfile, which is also shown in the first figure at the beginning of this articlefile (paper)whereasnew_folder
Then it is necessary to make a change to one of thesubfoldercomplementaryfile, which is also shown in the second figure at the beginning of this articlefile (paper)。
First, the overall idea of the code.
First of all, we based the()
function that traverses the standardfileEach of thesubfolder, get everysubfoldernames and store them in a list; next, we do the same thing to get the to-be-checked-outfilehit the nail on the headsubfolderThe names, again, are stored in a list. Next, we can start comparing the twofilecentersubfolderof the number of differences. First, set a variablenum
act assubfoldervariables for the computation of quantitative differences; subsequently, by means of afor
Loop and take out the standard in turnfilecentersubfolderthe name of the person who is to be checked for leaks in thefilecorrespondingsubfoldername list; if you can't find the current name of thesubfolderThis means that in the secondfileThat's missing from the list.subfolder, so it needs to be output with its name in the variablenum
add1
. After completing the above loop, we can get the secondfileThat is to say, it is to be checked and correctedfilein which the missingsubfolderThe name of the organization, as well as its number.
Next, the code is described in detail sentence by sentence as follows.
For the first part, we need to import the requiredPythonbuilt-in moduleos
In this article, it is used to interact with the operating system, which in this case is to perform operations such as reading a list of files.
Subsequently, we specify a folder path that is stored in the variabletemplate_folder
in it; this folder is the one we use as a standardfile, i.e., as shown in the first figure at the beginning of this articlefile (paper)Next. Next, we go ahead and specify another folder path, stored in the variablenew_folder
in it. This folder is the one in which thesubfoldercomplementaryfile, which is also shown in the second figure at the beginning of this articlefile (paper)。
Subsequently, using the()
function to get the data that is used as the standardfilein the list of all files and folders and store them in the variablefolder_list
in the program; in the same way, using the()
function gets a list of all the files and folders in another folder and stores it in the variablenew_list
Center.
Next, we initialize a variablenum
The variable is used to count the number of folders that exist in the template folder, but do not exist in the new folder. A loop can then be started to iterate through each folder in the template folder and use conditional judgment statements to check if this folder exists in the new folder - if the folder is not in the new folder, the following is done: in the first step, the variablenum
Increase in the value of1
, which is used to count the number of folders that do not exist in the new folder; in the second step, the name of the current folder is printed, along with additional text information.
Finally, we print the final result showing the number of folders that do not exist in the new folder.
Running the above code will give you the result shown below.
The code is very simple and ends here; if you have other needs, you can expand the code again on your own. For example, if you want to add thefilemissingsubfolderTo copy it over, then you can refer to the articlePython combines file names to copy multiple files to different pathsThe code ideas mentioned in are implemented.
At this point, the job is done.