如何仅将内容的差异从一个文件复制到另一个文件?

时间:2022-09-24 19:00:59

I have two files, source file and dest file. I want to copy the content from this source file to the dest file, but not the whole content. Only that part which is there in the source file and not in dest file.

我有两个文件,源文件和dest文件。我想将此源文件中的内容复制到dest文件,而不是整个内容。只有源文件中的那部分而不是dest文件中的部分。

I tried to search and came across shutil module but I couldn't find any function that copies only the diff of content from one file to other.

我试图搜索并遇到了shutil模块,但我找不到任何只将内容的差异从一个文件复制到另一个文件的函数。

How to do this in Python? Do we have any library function to accomplish this?

如何在Python中执行此操作?我们有任何库函数来实现这一目标吗?

Example:

source.txt:     dest.txt
a               a
b               c
c               e
d               f
e

After the desired operation, it should be:

在所需的操作之后,它应该是:

source.txt:     dest.txt
a               a
b               c
c               e
d               f
e               b
                d

Note that the order of lines doesn't matter.

请注意,行的顺序无关紧要。

2 个解决方案

#1


If you can arrange your files to be lists of lines, we can accomplish this very easily.

如果您可以将文件排列为行列表,我们可以非常轻松地完成此操作。

if len(lineList1) > len(lineList2):
    src = lineList1
    dst = lineList2
else
    src = lineList2
    dst = lineList1
for x in range(len(src)):
    if src[x] != dst[x]
        dst[x] = scr[x]

This snippet, finds the longest list, iterates over both, and if the line isn't the same on the destination, it is copied. Although I'm unsure the benefits of this approach over copying the file; except for practice.

此片段找到最长的列表,迭代两者,如果目标上的行不相同,则复制它。虽然我不确定这种方法比复制文件的好处;除了练习。

EDIT

I think I understand. Try this snippet:

我想我明白。试试这个片段:

output = dst + [x for x in src if x not in dst]

This iterates over every line and if its not in dst it's added at the end.

这会迭代每一行,如果它不在dst中,则会在最后添加。

#2


Use difflib

import difflib
file1 = "PATH OF FILE 1"
file1 = open(file1, "r")
file2 = "PATH OF FILE 2"
file2 = open(file2, "r")
diff = difflib.ndiff(file1.readlines(), file2.readlines())
file1.close()
file2.close()
delta = ''.join(x[2:] for x in diff if x.startswith('- '))
fh = open("PATH OF FILE 2", "a")
fh.write(delta)
fh.close
fh = open("PATH OF FILE 2","r")
print fh.read()
fh.close()

#1


If you can arrange your files to be lists of lines, we can accomplish this very easily.

如果您可以将文件排列为行列表,我们可以非常轻松地完成此操作。

if len(lineList1) > len(lineList2):
    src = lineList1
    dst = lineList2
else
    src = lineList2
    dst = lineList1
for x in range(len(src)):
    if src[x] != dst[x]
        dst[x] = scr[x]

This snippet, finds the longest list, iterates over both, and if the line isn't the same on the destination, it is copied. Although I'm unsure the benefits of this approach over copying the file; except for practice.

此片段找到最长的列表,迭代两者,如果目标上的行不相同,则复制它。虽然我不确定这种方法比复制文件的好处;除了练习。

EDIT

I think I understand. Try this snippet:

我想我明白。试试这个片段:

output = dst + [x for x in src if x not in dst]

This iterates over every line and if its not in dst it's added at the end.

这会迭代每一行,如果它不在dst中,则会在最后添加。

#2


Use difflib

import difflib
file1 = "PATH OF FILE 1"
file1 = open(file1, "r")
file2 = "PATH OF FILE 2"
file2 = open(file2, "r")
diff = difflib.ndiff(file1.readlines(), file2.readlines())
file1.close()
file2.close()
delta = ''.join(x[2:] for x in diff if x.startswith('- '))
fh = open("PATH OF FILE 2", "a")
fh.write(delta)
fh.close
fh = open("PATH OF FILE 2","r")
print fh.read()
fh.close()