Python 파일 write() 메서드
예제
追加로 파일을 "a"로 열고 몇 가지 텍스트를 파일에 추가합니다:
f = open("demofile2.txt", "a") f.write("See you soon!") f.close() #open and read the file after the appending: f = open("demofile2.txt", "r") print(f.read())
정의 및 사용법
write() 메서드는 지정된 텍스트를 파일에 씁니다.
지정된 텍스트가 삽입될 위치는 파일 모드와 스트림 위치에 따릅니다.
"a":텍스트가 현재 파일 스트림 위치에 삽입되며, 기본적으로 파일의 마지막에 삽입됩니다.
"w":현재 파일 스트림 위치(기본적으로 0)에 텍스트를 삽입하기 전에 파일을 비우습니다.
문법
파일.write(바이트)
파라미터 값
파라미터 | 설명 |
---|---|
바이트 | 삽입할 텍스트 또는 바이트 객체. |
더 많은 예제
예제
이전 예제와 동일하지만, 삽입할 텍스트 앞에 줄 바꿈을 삽입합니다:
f = open("demofile2.txt", "a") f.write("\nSee you soon!") f.close() #open and read the file after the appending: f = open("demofile2.txt", "r") print(f.read())