# 게시글 로딩하기
# data.csv 파일 있으면 게시글을 로딩
# data.csv 파일이 없으면 파일을 만든다.

# 1. data.csv를 읽는다.
# 2. 데이터 한 줄씩 읽고 한줄마다 Post 인스턴스를 만든다.
# 3. Post 리스트에 인스턴스를 저장한다.

import os
import csv
from post import Post

# 파일 경로
file_path = "./myvenv/Chapter12/data.csv"

# Post 객체를 저장 할 리스트
post_list = []


# data.csv 파일이 있다면
if os.path.exists(file_path):
    # 게시글 로딩
    print("게시글 로딩중입니다.")
    # csv 파일 읽기
    f = open(file_path, "r", encoding="utf-8")
    # reader로 하면 한 줄씩 읽어온다
    reader = csv.reader(f)
    for data in reader:
       # Post 인스턴스 생성하기
        post = Post(int(data[0]), data[1], data[2], int(data[3]))
        # post 리스트에 append하기
        post_list.append(post)

else:
    # 게시글이 없다면 파일 생성하기
    f = open(file_path, "w", encoding="utf-8", newline="")
    f.close()

# print(post_list[0].get_title())
# print(post_list[0].get_content())
# print(post_list[0].get_view_count())

# 게시글 등록
# 1. Post 인스턴스 생성
# 2. Post 리스트에 저장

# Post 인스턴스
# 1.글번호 2.제목 3.내용 4.조회수
def write_post():
    """
    게시글 쓰기 함수
    """
    print("\n\n -게시글 쓰기")
    title = input("제목을 입력해주세요 \n >>>")
    content = input("내용을 입력해주세요 \n >>>")
    # 글번호
    id = post_list[-1].get_id() + 1
    post = Post(id, title, content, 0)
    post_list.append(post)
    print(" # 게시글이 등록되었습니다.")

# 게시글 목록
def list_post():
    """
    게시글 보기
    """

# 메뉴 출력하기
while True:
    print("\n\n- FASTCAMPUS BLOG")
    print("- 메뉴를 선택해주세요")
    print("1. 게시글 쓰기")
    print("2. 게시글 등록하기")
    print("3. 프로그램 종료")

# 문자를 썼을때 에러가 발생
    try:
        choice = int(input(">>>"))
    except ValueError:
        print("숫자를 입력해주세요")    
    else:      
        if choice == 1:
            write_post()
        elif choice == 2:
            list_post()
            print("게시글 목록")
        elif choice == 3:
            print("프로그램 종료")
            break

 

 

https://bit.ly/37BpXiC

 

패스트캠퍼스 [직장인 실무교육]

프로그래밍, 영상편집, UX/UI, 마케팅, 데이터 분석, 엑셀강의, The RED, 국비지원, 기업교육, 서비스 제공.

fastcampus.co.kr

본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성되었습니다.

 

BELATED ARTICLES

more