본문 바로가기
Python

주소록 만들기

by 헬로제이콥 2025. 12. 24.
import os

# 저장할 주소록 파일 경로
file_path = r"d:\Python_ccm\address_book.txt"

while True:
    print("\n" + "★"*15)
    print("   학생 주소록 관리자")
    print("★"*15)
    print("1. 주소록 파일 만들기 (신규 생성)")
    print("2. 학생 새로 입력 (기존 내용 초기화)")
    print("3. 학생 추가하기 (내용 덧붙이기)")
    print("4. 주소록 읽어오기 (전체 명단 보기)")
    print("5. 프로그램 종료 (q)")
    print("="*30)
   
    choice = input("원하는 작업의 번호를 입력하세요: ")
   
    if choice.lower() == 'q':
       print("\n주소록 프로그램을 종료합니다. 수고하셨습니다!")
       break
    elif choice == '1':
            with open(file_path, "x", encoding="utf-8") as f:
                f.write("=== [학생 주소록 데이터] ===\n")
            print("\n[성공] 새로운 주소록 파일이 생성되었습니다.")
    elif choice == '2':
            name = input("학생 이름: ")
            phone = input("전화 번호: ")
            with open(file_path, "w", encoding="utf-8") as f:
                f.write("=== [학생 주소록 데이터] ===\n")
                f.write(f"이름: {name} / 연락처: {phone}\n")
            print(f"\n[성공] '{name}' 학생의 정보로 주소록이 초기화되었습니다.")
    elif choice == '3':
            name = input("추가할 학생 이름: ")
            phone = input("추가할 전화 번호: ")
            with open(file_path, "a", encoding="utf-8") as f:
                f.write(f"이름: {name} / 연락처: {phone}\n")
            print(f"\n[성공] '{name}' 학생의 정보가 추가되었습니다.")        
    elif choice == '4':        
            print("\n" + "-"*10 + "[ 전체 주소록 명단 ]" + "-"*10)
            with open(file_path, "r", encoding="utf-8") as f:
                print(f.read())
            print("-" * 40)    
    else:
            print("\n[알림] 번호를 잘못 입력하셨습니다. (1~5번 입력)")