current position:Home>How does Python compare file differences between two paths

How does Python compare file differences between two paths

2022-01-30 03:42:31 Clever crane

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

We may encounter such a situation , There are two folders , Most of the files inside are the same , A small number of inconsistencies , We need to find the different files in the two directories .

The main function used is os.wall() and shutil.copy() , The former is used to traverse folders , The latter is used to copy files .

1. Traverse the directory

os.walk() The usage of functions was mentioned in the previous article , Use it here to path Traverse all files under the path to find .

fileList = []
for root, dirs, files in os.walk(path):
    for fileName in files:
        file = os.path.join(root, name)
        fileList.push(file)
print(fileList)
 Copy code 

2. Compare the differences between the two directories

The idea is , First traversal path1 Get all files under this path , Then traverse path2 when , Judge path2 Is your file in path1 In the file list .

def compareDirs(path1, path2):
    fileList = []
    for root, dirs, files in os.walk(path1):
        for fileName in files:
            file = os.path.join(root, name)
            #  Remove  path1  route , Keep only file relative paths 
            file = file.replace(path1, "")
            fileList.push(file)
	
    diffList = []
	for root, dirs, files in os.walk(path2):
        for fileName in files:
            file = os.path.join(root, name)
            file = file.replace(path1, "")
            if file not in fileList:
            	diffList.push(file)
	return diffList
 Copy code 

if path2 The file in is not in path1 in , Then put the file into diffList Back in .

3. Copy difference file

If you need to copy the different files to a new folder , You can refer to the following code .

import shutil

def copyFiles(srcDir, destDir, fileList):
    for file in fileList:
        #  You need to extract the file path that does not contain the file name 
        destPath = destDir + "/".join(file.split("\\")[0:-1]) + "/"
        if not os.path.exists(destPath):
            os.makedirs(destPath)
        shutil.copy(srcDir + file, destPath)
 Copy code 

4. Complete code

The complete reference code is as follows :

import os, shutil

def compareDirs(path1, path2):
    fileList = []
    for root, dirs, files in os.walk(path1):
        for fileName in files:
            file = os.path.join(root, name)
            #  Remove  path1  route , Keep only file relative paths 
            file = file.replace(path1, "")
            fileList.push(file)
	
    diffList = []
	for root, dirs, files in os.walk(path2):
        for fileName in files:
            file = os.path.join(root, name)
            file = file.replace(path1, "")
            if file not in fileList:
            	diffList.push(file)
	return diffList

def copyFiles(srcDir, destDir, fileList):
    for file in fileList:
        #  You need to extract the file path that does not contain the file name 
        destPath = destDir + "/".join(file.split("\\")[0:-1]) + "/"
        if not os.path.exists(destPath):
            os.makedirs(destPath)
        shutil.copy(srcDir + file, destPath)
        
if __name__ == "__main__":
    path1 = "D://data1"
    path2 = "D://data2"
    destPath = "D://data3"
    diff = compareDirs(path1, path2)
    copyFiles(path2, destPath, diff)
 Copy code 

copyright notice
author[Clever crane],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300342290770.html

Random recommended