file
파일 열기
open(파일명, 모드, 인코딩)
| Character | Meaning |
|---|---|
| 'r' | open for reading (default) |
| 'w' | open for writing, truncating the file first |
| 'x' | open for exclusive creation, failing if the file already exists |
| 'a' | open for writing, appending to the end of file if it exists |
| 'b' | binary mode |
| 't' | text mode (default) |
| '+' | open for updating (reading and writing) |
f = open('python.txt', mode='r', encoding=None)
print(f)
>>> <_io.TextIOWrapper name='python.txt' mode='r' encoding='UTF-8'>
print(f.read())
>>> py
>>> thon
f.close()
# open 했으면 close 해야 데이터 유실 없음
or
파일 쓰기
write
with open('python.txt', 'w') as f:
f.writeline(['abc', 'def\n', 'gh'])
# writelines 는 리스트, 튜플 같은 문자열 컬렉션으로 입력
파일 읽기
read
with open('python.txt', 'r') as f:
while True:
x = f.read()
if x == '': # while 문 탈출이 필요하기에 x 가 빈 문자열이면 break
break
print(x)
>>> py
>>> thon
# read 는 한 글자씩 읽어오며 더이상 읽을 내용 없으면 종료