0

ソースファイルのすべての行が(グループとして)宛先ファイルにまだ存在しないかどうかを確認した後、宛先ファイルに追加する必要があるコンテンツを含むソースファイルがあります。

宛先ファイルに既に見つかっている場合は、宛先ファイルの内容が重複するため、再度追加しないでください。

これは基本的に、行のブロック全体を比較しています。正規表現を使用せずにPythonでこれを行う方法はありますか?

4

2 に答える 2

2
src = open('source').read()
if src not in open('dest').read():
    with open('dest', 'a') as dst:
        dst.write(src)
于 2013-03-13T12:19:35.187 に答える
0

ファイル全体を単一の文字列としてメモリにロードできる場合は、次のように単純に使用できますcount

import os

f = open("the_file_name", 'r+')
s1 = "the block of text\nwith newlines!\nyou will search in the file"
s2 = f.read() # s2 now has the whole file
if s2.count(s1) > 0:
    # seek to end and append s1
    f.seek(0, os.SEEK_END)
    f.write(s1)
于 2013-03-13T12:01:38.773 に答える