J o e
JoE's StOrY
J o e
  • 분류 전체보기 (206)
    • workSpace (184)
      • 도메인 지식 (2)
      • ALGORITHM (39)
      • ANDROID (3)
      • JS (0)
      • JAVA (21)
      • MYSQL (6)
      • NETWORK (3)
      • PYTHON (91)
      • LINUX (9)
      • PROJECT (4)
    • Others (20)
      • Opic (1)
      • myLife (17)
      • popSong (1)
      • 정보처리기사 (1)
    • 훈빠의 특강 (0)
      • opencv (0)
      • python (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • The code with long statements is⋯
  • 매일 매일이 행복하고 밝은 날이 될거에요

인기 글

태그

  • full loss
  • DTO
  • numpy
  • linearclassification
  • sort_value
  • How to create a GUI in Java with JFrame?
  • sort_index
  • dao
  • java
  • 태블릿 연동
  • MySQL
  • ㅖ43
  • Python
  • 넘파이 문제
  • 넘파이함수
  • 이미지 연산
  • Fully Connected Network
  • 단어의 개수
  • read_html
  • 파이썬

최근 댓글

최근 글

티스토리

J o e

WHY?

[Python] 파일 생성 수정 삭제
workSpace/PYTHON

[Python] 파일 생성 수정 삭제

2020. 12. 26. 20:26
# 메모라는 디렉토리가 없으면 생성
# 메뉴 : 1.읽기 2.쓰기 3.삭제. 4.전체삭제
# 읽기 : 파일명 읽어서 파일 내용 보여주기
# 쓰기 : 파일명을 입력받는데 중복되면 안됨, 저장할 내용 입력받음.
# 삭제 : 파일 목록을 보여주고 삭제할 파일 선택 후 삭제
# 전체 삭제 : 메모 디렉토리 삭제
# 파일 있으면 '이어쓰기'
import os

def print_menu():
    print("==== 메뉴 ====")
    print("1. 읽기")
    print("2. 쓰기")
    print("3. 삭제")
    print("4. 전체 삭제")
    print("5. 종료")
    print("=============")

def proc_read():
    filename = input("파일 이름:")
    if not os.path.isfile(filename):
        print("해당 파일이 없습니다.")

    f = open(filename, "r", encoding='utf-8')
    print(f.name)
    print(f.read())
    f.close()

def proc_write():
    filename = input("파일 이름:")
    write_mode = 'w+'
    if os.path.isfile(filename):
        while True:
            i = input("파일이 이미 존재합니다. 이어쓰시겠습니까? (Y/N)")
            if i == "Y" or i == "y":
                write_mode = 'a+'
                break
            elif i == 'N' or i == 'n':
                return

    f = open(filename, write_mode, encoding='utf-8')
    while True:
        print(f.name)
        print(f.read())
        i = input("입력:")
        print("('/stop'으로 입력을 끝낼 수 있습니다.)")
        if i == '/stop':
            break
        f.writelines(i+"\n")
    print(f.read())
    f.close()
    f = open(filename, 'r', encoding='utf-8')
    print(f.read())


def proc_remove():
    for i in os.listdir():
        print(i)
    filename = input("파일 이름:")
    if os.path.isfile(filename):
        os.remove(filename)
    else:
        "해당 파일이 없습니다."

def proc_clear():
    for i in os.listdir():
        os.remove(i)
    os.chdir("../")
    os.rmdir("memo")
    return False

def proc_exit():
    return False

def check_dir():
    if not os.path.isdir("memo"):
        os.mkdir("memo")
    os.chdir("memo")

def main():
    print("현재 디렉토리:", os.getcwd())

    if not os.path.isdir("memo"):
        os.mkdir("memo")
    os.chdir("memo")

    while True:
        print_menu()
        proc = [proc_read,proc_write,proc_remove,proc_clear,proc_exit]
        i = input("선택:")
        if i.isdigit():
            if proc[int(i)-1]() == False:
                break


main()

'workSpace > PYTHON' 카테고리의 다른 글

[Python][firebase][os] 파일을 활용한 정보처리기사 프로그램  (0) 2020.12.26
[Python] 제품입고정리 프로그램 (MVC pattern)  (0) 2020.12.26
[Python] 주소록 프로그램  (0) 2020.12.26
[Python] 제품입출력 프로그램 (MVC pattern)  (0) 2020.12.26
[Python] 성적입출력 프로그램 (MVC pattern)  (0) 2020.12.26
    'workSpace/PYTHON' 카테고리의 다른 글
    • [Python][firebase][os] 파일을 활용한 정보처리기사 프로그램
    • [Python] 제품입고정리 프로그램 (MVC pattern)
    • [Python] 주소록 프로그램
    • [Python] 제품입출력 프로그램 (MVC pattern)
    J o e
    J o e
    나의 과거를 기록합니다.

    티스토리툴바