//=$secondUrl?>
Python
기존 파일 내용 유지하면서 파일에 내용쓰기
기존 파일 내용 유지하면서 파일에 내용쓰기
앞에서 파일에 내용을 작성했습니다.
이번에는 그 파일에 기존의 내용은 유지하면서 내용을 쓰는 방법에 대해 알아보겠습니다.
이미 우리가 알고 있습니다. 파일을 열 때 a또는 at를 사용합니다.
없는 파일은 생성하고 기존 파일은 기존 내용을 유지하고 내용 끝에 내용을 붙입니다.
기존 파일 내용을 유지하면서 내용 쓰기
f = open('pleaseMakeMe.txt','a') if f: f.write("hello world") f.close() else: print('file open failed')
그럼 위의 파일을 실행해서 pleaseMakeMe.txt를 확인합시다.

결과를 보니 기존 내용인 hello world가 있는 상태에서 hello world가 붙어서 정상적으로 작동했습니다.
하지만 별로 보기가 좋지 않죠? 줄을 바꿔서 넣어봅시다.
이스케이프 코드 중 줄바꿈을 하는 기능인 \n을 hello world앞에 넣어서 해봅시다.
f = open('pleaseMakeMe.txt','a') if f: f.write("\nhello world") f.close() else: print('file open failed')
다시 실행을 해봅시다.

이번엔 줄이 바뀌어 내용이 작성되었습니다.
그럼 다음시간에는 pleaseMakeMe.txt 파일의 내용을 읽어봅시다.
//=$langList['bottomThankyou'][$langMode]?>