Python 입력과 출력

Coding-Luizy·2022년 7월 20일

python

목록 보기
8/9
post-thumbnail

open함수

외부 파일을 읽고 쓰고 수정하기위해 사용하는 기본 함수이다.

def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): 

mode='w'

  • write
    파일을 열고 없다면 생성시켜 처음부터 쓰기 시작한다.(내용이 있었을경우 기존 내용이 사라진다.)

mode='r'

  • read
    readline()은 한줄씩읽어서 문자열로 반환한다.

    readlines()은 모든 줄을읽어서 줄별로 문자열 리스트로 반환한다.

문자열 strip함수의 활용


mode='a'

  • add
    파일 맨 마지막부터 새롭게 내용을 작성하기 시작한다.

with문


f = open("foo.txt", 'w')
f.write("Life is too short, you need python")
f.close()
with open("foo.txt", "w") as f:
    f.write("Life is too short, you need python")
profile
Better Tomorrow

0개의 댓글