프로그래밍
[python] 생활코딩 - TXT 파일 글자수로 나누기
진박사.
2021. 5. 9. 02:16
file_path = r"" #txt 파일 절대경로
output_path = r"" #파일 출력 경로
chunck_size = 100 #나눌 글자수
f = open(file_path,"rt", encoding='UTF8')
line = f.read()
f.close()
def chuncks(s, n):
for start in range(0, len(s), n):
yield s[start:start+n]
i = 1
for chunck in chuncks(line, chunck_size):
o = open(output_path + f"\{i}.txt", "w")
o.write(chunck)
i = i+1
o.close()
file_path : 나눌 txt 파일 절대 경로 ex) C:\temp\temp.txt
output_path : 나눈 txt 파일이 저장될 경로 ex)C:\temp\output
chunck_size : txt파일을 몇글자씩 나눌 것인가?
ex) 5로 설정시 '가나다라마바사아자차카타파하' -> '가나다라마', '바사아자차', '카타파하' 로 분할되어 각각 1.txt 2.txt 3.txt로 출력됨