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

[file] Python - write() 메서드 ★★★ - 파일 쓰기. (= write메서드 = 라이트)

1632  
목차
  1. write() 예제 - 파일 쓰기
  2. write() 정의
  3. write() 구문
  4. write() 예제 - 줄바꿈 후, 파일 쓰기

 

write() 예제 - 파일 쓰기

 

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

f.write("홈짱닷컴 Homzzang.com")

f.close()

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

print(f.read())

 

결과값: 홈짱닷컴 Homzzang.com

 

write() 정의

 

파일에 텍스트 쓰기.

 


 

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

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

 

'a' 옵션 : 

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

파일 없으면 생성.

 

'w' 옵션 : 

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

파일 없으면 생성.

 

 

write() 구문

 

file.write(byte)

 


[매개변수]

 

byte

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

 

 

write() 예제 - 줄바꿈 후, 파일 쓰기

 

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

f.write("\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