Python 파일 writelines() 메서드
예시
추가를 위해 "a"가 있는 파일을 연 다음 파일에 추가할 텍스트 목록을 추가합니다.
f = open("demofile3.txt", "a")
f.writelines(["See
you soon!", "Over and out."])
f.close()
#open
and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())
정의 및 사용
이 writelines()
메서드는 목록의 항목을 파일에 씁니다.
텍스트가 삽입되는 위치는 파일 모드와 스트림 위치에 따라 다릅니다.
"a"
: 텍스트는 현재 파일 스트림 위치에 삽입되며 기본값은 파일 끝입니다.
"w"
: 텍스트가 현재 파일 스트림 위치(기본값 0)에 삽입되기 전에 파일이 비워집니다.
통사론
file.writelines(list)
매개변수 값
Parameter | Description |
---|---|
list | The list of texts or byte objects that will be inserted. |
더 많은 예
예시
위와 같은 예이지만 각 목록 항목에 줄 바꿈을 삽입합니다.
f = open("demofile3.txt", "a")
f.writelines(["\nSee
you soon!", "\nOver and out."])
f.close()
#open
and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())