Aa
aadevcomm

Code / Tip 공유 게시판

📣 내가 만든 파이썬 코드 / 나만 알고 있는 AA Tip 을 공유해주세요.
지식은 나눠야 배가 된다고 했어요.🤣🤣🤣
🔶Code는 "/코드" 를 사용해서 입력해주세요.
🔶파일 업로드 용량제한(50MB)이 있습니다.

📌 Apeople : https://apeople.automationanywhere.com
📌 BotStore : https://botstore.automationanywhere.com
[python] 프로그램 실행
  • 김
    김영호_메가존
2023년 10월 24일 오후 3:29
[python] Chat GPT
  • 김
    김영호_메가존
2023년 10월 24일 오후 3:12
[python] ScreenCapture
  • 김
    김영호_메가존
2023년 10월 24일 오후 1:57
[python] Recording
  • 김
    김영호_메가존
2023년 10월 24일 오전 11:14
[python] txt edit
  • 김
    김영호_메가존
2023년 10월 24일 오전 10:54
(예시) [Python] Mail Send
  • 강
    강민석_메가존
2023년 8월 1일 오후 3:25
🙏👏
2
Aa
aadevcomm
Code / Tip 공유 게시판
이미지 없음

[python] txt edit

김
김영호_메가존
2023년 10월 24일3년 전
이미지 없음
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("오픈채팅방입니다.") // 파라미터는 각 매소드마다 확인 후 추가
👍