본문 바로가기
Python/☞

[59]-A. 콘솔 게시판

by TR. 2020. 9. 23.
import os

# 게시판

fileName = "datas.txt"

contents = []
cnt = 0
 
SIZE = 5
PAGE = 1

curPage = 1
startIdx = 0
endIdx = 4

run = True
while run :
    datas = ""
    
    # 파일로드
    if os.path.exists(fileName) :
        file = open(fileName, "rt")
        datas = file.read()
        file.close()

        info = datas.split('\n')
        cnt = int(info[0])
        if cnt != 0:
            PAGE = cnt // SIZE
            if cnt % SIZE != 0 :
                PAGE += 1

            if cnt <= SIZE :
                endIdx = cnt-1
                
            contents = []
            for i in range(1,cnt+1) :
                temp = info[i].split(',')
                contents.append(temp)
    else :
        file = open(fileName, "w")

    # 메뉴출력
    print("---- BOARD ----")
    print("[%d/%d page]" % (curPage, PAGE))
    print("[게시글: %d개]" % cnt)
##    print("ㄴ start: ", startIdx)
##    print("ㄴ end: ", endIdx)

    if datas in "" :
        print("No Data")
    else :
        for i in range(startIdx, endIdx +1) :
            print("[%d] %s" % (i+1, contents[i][0]))
        

    print("---------------")
    print("[숫자] 읽기")
    print("[ > ] 다음")
    print("[ < ] 이전")
    print("[add] 글쓰기")
    print("[set] 수정")
    print("[del] 삭제")

    sel = input("입력 : ")

    if sel in ">" :
        if curPage != PAGE :
            startIdx += SIZE
            if curPage +1 == PAGE and cnt % SIZE != 0 :
                endIdx += (cnt % SIZE)
            else :
                endIdx += SIZE
            curPage += 1

    elif sel in "<" :
        if curPage != 1 :
            startIdx -= SIZE
            endIdx = startIdx + SIZE-1
            curPage -= 1

    elif sel in "add" :
        title = input("제목 입력 : ")
        if title in "" :
            continue
        text = input("내용 입력 : ")
        if text in "" :
            continue
        
        add = [title, text]
        contents.append(add)  
        cnt += 1

        # 파일 저장
        datas = str(cnt) + "\n"
        for i in range(len(contents)) :
            datas += contents[i][0] + ',' + contents[i][1]
            if i != len(contents) -1 :
                datas += '\n'
        file = open(fileName, "wt")
        file.write(datas)
        file.close()

    elif sel in "set" :
        idx = int(input("수정할 게시글 선택 : ")) -1
        if idx >= 0 and idx < cnt :
            sel2 = int(input("1.제목\n2.내용\n"))
            setData = []
            if sel2 == 1 :
                title = input("제목 : ")
                setData.append(title)
                setData.append(contents[idx][1])
            elif sel2 == 2 :
                text = input("내용 : ")
                setData.append(contents[idx][0])
                setData.append(text)
            contents[idx] = setData

            # 파일 저장
            datas = str(cnt) + "\n"
            for i in range(len(contents)) :
                datas += contents[i][0] + ',' + contents[i][1]
                if i != len(contents) -1 :
                    datas += '\n'
            file = open(fileName, "wt")
            file.write(datas)
            file.close()
    
    elif sel in "del" :
        idx = int(input("삭제할 게시글 선택 : ")) -1
        del contents[idx]
        cnt -= 1

        # 파일 저장
        datas = str(cnt) + "\n"
        for i in range(len(contents)) :
            datas += contents[i][0] + ',' + contents[i][1]
            if i != len(contents) -1 :
                datas += '\n'
        file = open(fileName, "wt")
        file.write(datas)
        file.close()

    # 게시글 조회
    else :
        idx = int(sel) -1
        print("---------------")
        print("제목: ", contents[idx][0])
        print("내용: ", contents[idx][1])
        
        

'Python > ' 카테고리의 다른 글

[59] 콘솔 게시판  (0) 2020.09.23
[58] 파일 쓰기/읽기  (0) 2020.09.23
[57]-A. 영화관 예매 (함수)  (0) 2020.08.28
[57] 영화관 예매 (함수)  (0) 2020.08.28
[56] 딕셔너리  (0) 2020.08.25

댓글