• 회원가입
  • 로그인
  • 구글아이디로 로그인

[file] Python - writelines() 메서드 ★★★ - 파일에 List 요소 쓰기. (= writelines메서드 = 라이트라인즈)

3742  
목차
  1. writelines() 예제 - 파일에 List 요소 쓰기
  2. writelines() 정의
  3. writelines() 구문
  4. writelines() 예제 - 파일에 List 요소 줄바꿈 후 쓰기

 

writelines() 예제 - 파일에 List 요소 쓰기

 

f = open("hz.txt", "a", encoding='UTF-8')

f.writelines(["홈짱닷컴", "Homzzang.com"])

f.close()

f = open("hz.txt", "r", encoding='UTF-8')

print(f.read())

 

결과값: 홈짱닷컴 Homzzang.com

 

writelines() 정의

 

파일에 List 요소를 쓰기.

 


 

텍스트 삽입 위치는 파일 모드 및 스트림 위치에 따라 다름.

※ open() 함수로 파일 열 때, 'a' 나 'w' 옵션 사용 시 쓰기 가능. 

 

※ 'a' 옵션 : 

텍스트는 현재 파일 스트림 위치에 삽입. 기본적으로 파일 끝에 추가.

파일 없으면 생성.

 

※ 'w' 옵션 : 

현재 파일 스트림 위치 (기본값 0)에 텍스트 삽입 전 파일 비움.

파일 없으면 생성.

 

 

writelines() 구문

 

file.writelines(list)

 


[매개변수]

 

list

필수. 삽입될 텍스트 (또는 byte 객체)의 List

 

 

writelines() 예제 - 파일에 List 요소 줄바꿈 후 쓰기

 

f = open("hz.txt", "a", encoding='UTF-8')

f.writelines(["\n홈짱닷컴", "\nHomzzang.com"])

f.close()

f = open("hz.txt", "r", encoding='UTF-8')

print(f.read())

 

결과값: 

홈짱닷컴

Homzzang.com



분류 제목
module Python - statistics.mode() 메서드 ★ - 최빈값 반환. (= mode메서드 = 모드)
module Python - statistics.pstdev() 메서드 -
module Python - statistics.stdev() 메서드 -
module Python - statistics.pvariance() 메서드 -
module Python - statistics.variance() 메서드 -
module Python - math 모듈 메서드・상수 종류 (※ 수학 모듈)
module Python - math.acos() 메서드 -
module Python - math.acosh() 메서드 -
module Python - math.asin() 메서드 -
module Python - math.asinh() 메서드 -
module Python - math.atan() 메서드 -
module Python - math.atan2() 메서드 -
module Python - math.atanh() 메서드 -
module Python - math.ceil() 메서드 ★ - 천장 반올림 (= 상위 정수로 반올림. = ceil메서드…
module Python - math.comb() 메서드 -
module Python - math.copysign() 메서드 -
module Python - math.cos() 메서드 -
module Python - math.cosh() 메서드 -
module Python - math.degrees() 메서드 -
module Python - math.dist() 메서드 -
19/24
목록
찾아주셔서 감사합니다. Since 2012