[Python]연속된 문자열을 파일에 쓰는 예제
파이썬을 이용하여 연속된 문자열을 파일에 써보는 방법을 알아보자.
## 요구사항 파일명 & 경로 : "../source/23-1.txt" 파일 출력 결과 : "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" # 방법1 def write_alphabet1(filepath): with open(filepath, "w") as file: for letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': # str은 시퀀스형 file.write(letter + " ") write_alphabet1("../source/23-1.txt") # 방법2 import string def write_alphabet2(filepath): with open(filepath, "w") as file: for letter in string.ascii_uppercase: file.write(letter + " ") write_alphabet2("../source/23-2.txt")
최신 댓글