class TextEditor:
def **init**(self, file_path):
self.file_path = file_path
def read_file(self):
"""파일의 내용을 문자열로 반환합니다."""
with open(self.file_path, 'r', encoding='utf-8') as file:
return file.read()
def write_file(self, content):
"""문자열 내용을 파일에 저장합니다."""
with open(self.file_path, 'w', encoding='utf-8') as file:
file.write(content)
def replace_text(self, old_text, new_text):
"""파일 내의 특정 텍스트를 다른 텍스트로 교체합니다."""
content = self.read_file()
content = content.replace(old_text, new_text)
self.write_file(content)
def remove_text(self, target_text):
"""파일 내의 특정 텍스트를 제거합니다."""
self.replace_text(target_text, '')
def find_lines_with_text(self, target_text):
"""파일 내에서 특정 텍스트를 포함하는 모든 라인을 반환합니다."""
with open(self.file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
matching_lines = [line for line in lines if target_text in line]
return matching_lines
def delete_line(self, line_number):
"""주어진 라인 번호의 라인을 파일에서 삭제합니다."""
with open(self.file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
if 0 < line_number <= len(lines):
del lines[line_number - 1]
self.write_file(''.join(lines))
else:
print("Invalid line number.")
// 메소드 사용시
if __name__ == "__main__":
path = "" // 편집하려는 txt파일 Full경로
editor = TextEditor(path)
editor.find_lines_with_text("오픈채팅방입니다.") // 파라미터는 각 매소드마다 확인 후 추가