
xdiff_file_patch 함수에서 변경된 시점에만 patch를 생성하는 옵션은 XFRM_DIFF_DELETE_EMPTY_LINES 옵션입니다.
이 옵션을 사용하여 patch를 생성하는 방법은 다음과 같습니다.
1. XFRM_DIFF_DELETE_EMPTY_LINES 옵션을 True로 설정하여 xdiff_file_patch 함수를 호출합니다.
#hostingforum.kr
python
from difflib import Differ
differ = Differ()
old_file = "old_file.txt"
new_file = "new_file.txt"
with open(old_file, 'r') as old, open(new_file, 'r') as new:
patches = differ.compare(old.readlines(), new.readlines())
with open("patch.txt", 'w') as patch_file:
for patch in patches:
if patch.startswith('- ') or patch.startswith('+ '):
patch_file.write(patch + 'n')
2. 변경된 시점에만 patch를 생성하기 위해, patch를 생성하기 전에 이전 파일의 내용과 현재 파일의 내용을 비교하여 변경된 라인만 patch에 포함합니다.
#hostingforum.kr
python
from difflib import Differ
differ = Differ()
old_file = "old_file.txt"
new_file = "new_file.txt"
with open(old_file, 'r') as old, open(new_file, 'r') as new:
patches = differ.compare(old.readlines(), new.readlines())
with open("patch.txt", 'w') as patch_file:
for patch in patches:
if patch.startswith('- ') or patch.startswith('+ '):
if patch.startswith('- ') and patch[2:].strip() != '':
patch_file.write(patch + 'n')
elif patch.startswith('+ ') and patch[2:].strip() != '':
patch_file.write(patch + 'n')
이러한 방법으로, xdiff_file_patch 함수를 사용하여 파일의 내용이 변경된 시점에만 patch를 생성할 수 있습니다.
2025-03-11 12:04