logo
Search검색어를 포함하는 게시물들이 최신순으로 표시됩니다.
    Table of Contents
    [Python] With

    이미지 보기

    [Python] With

    • 21.07.16 작성

    • 읽는 데 2

    TOC

    With 문

    1. pickle을 이용하는 방법

    • file의 정보를 읽어와서 변수에 저장하는 방법
    • with open("file", "rb") as variable:
    • with 문을 탈출하면서 profile.pickle file을 종료하므로 따로 .close() 문을 쓸 필요 없다.
    import pickle
    with open("profile.pickle", "rb") as profile_file:
        # profile.pickle에서 데이터를 읽어와서 profile_file 변수에 값 저장
    
        print(pickle.load(profile_file))
    
    # 출력값 : {'이름': '박명수', '나이': 30, '취미': ['축구', '골프', '코딩']}
    

    2. pickle이 아닌 일반 파일을 이용하는 방법 - 덮어쓰기

    with open("study.txt", "w", encoding="utf8") as study_file:
        study_file.write("파이썬을 열심히 공부하고 있어요.")
    
    # 실행결과
    # study.txt 파일이 같은 디렉토리 내에 생기고, '파이썬을 열심히 공부하고 있어요.' 라는 문장이 있다.
    

    3. pickle이 아닌 일반 파일을 이용하는 방법 - 불러오기

    with open("study.txt", "r", encoding="utf8") as study_file:
        print(study_file.read())
    
    # 출력값 : 파이썬을 열심히 공부하고 있어요.
    

    Reference

    profile

    FE Developer 박승훈

    노력하는 자는 즐기는 자를 이길 수 없다