Notice
Recent Posts
Recent Comments
Link
목록2025/02/19 (1)
아미(아름다운미소)
파일에 최대 5줄까지만 유지하면서 새로운 내용을 추가
def write_limited_lines(file_path, new_line, max_lines=5): # 기존 파일 읽기 try: with open(file_path, "r", encoding="utf-8") as f: lines = f.readlines() except FileNotFoundError: lines = [] # 기존 내용에서 마지막 (max_lines - 1) 줄만 유지 lines = lines[-(max_lines - 1):] # 새로운 줄 추가 lines.append(new_line + "\n") # 파일 다시 쓰기 with open(file_path, "w", encoding="utf..
랭귀지/python
2025. 2. 19. 09:17