728x90
반응형
[io01_file 쓰기(만들기)]
file = open('test01.txt', 'w')
file.write('hello, world!')
file.close()
print('끝!')
'''
r: 읽기
w: 쓰기(기존 내용 덮어쓰기)
a: 쓰기(기존 내용 이후에 쓰기)
x: 새로운 파일 만들어서 쓰기(이미 파일이 있으면 에러)
t / b: text /binary(default: t)
'''
[io02_file읽기]
위에서 만든 파일 안에 담긴 내용을 읽어준다.
file = open('test01.txt', 'r')
a = file.read()
print(a)
file.close()
# 결과값
hello, world!
[io03_file읽기_자동닫기]
with을 사용하면 try-catch의 finally에 close를 해준것과 같이 실행되고 마지막에 close를 자동으로 해준다.
with open('test01.txt','r') as file:
a = file.read()
print(a)
[io04_file쓰기(이어서쓰기)] : a, append
만들어진 파일 내에 입력한 내용을 이어 써준다.
file = open('test01.txt','a')
file.writelines('\nhello, python!')
file.close()
728x90
반응형
'Language > Python' 카테고리의 다른 글
[python]crawling_크롤링_naver01 (0) | 2020.10.12 |
---|---|
[python]io_pickle (0) | 2020.10.12 |
[python]math_원의 넓이 (0) | 2020.10.12 |
[python]numpy, matplotlib_그래프 (0) | 2020.10.12 |
[python]random_랜덤, 로또 (0) | 2020.10.12 |