write2file.py
파일을 읽어 와서 한 파일에 쓰기
a.txt 라는 파일이 있을 때
open('a.txt').read() # a.txt 파일 열어서 읽기
infile = open('a.txt') # infile 에 파일 자체? 넣기
infile.read() # infile 읽기
close(infile)
infile = open('a.txt').read() # infile 에 a.txt 내용 넣기
infile.close() # infile 은 str 이니까 못닫음
a.txt 파일과 b.txt 파일 읽어서 c.txt 파일에 쓰기
#coding: utf-8
print('a.txt 와 b.txt 를 읽어와서 c.txt 에 적기')
infile = open('a.txt', 'r')
data = infile.read()
infile.close()
print('a.txt: '+ data)
infile1 = open('b.txt', 'r')
data1 = infile1.read()
infile1.close()
print('b.txt: ' + data1)
ofile = open('c.txt', 'w')
ofile.write(data)
ofile.write(data1)
ofile.close()
lines = [data, data1]
ofile.writelines(lines) # 데이터들 한 줄씩 한 번에 삽입 가능
불러온 파일과 형식의 type 확인해보면
print(type(infile1), type(data1))
print(type(onfile), type(lines))
파일을 open 하면 위 클래스처럼 나오고 각각 데이터 type 은 str, list 형식이다.
'수업정리 > Fundamental' 카테고리의 다른 글
[Python] 한글 유니코드 3 - Hangul.py (인코딩,디코딩) (0) | 2023.04.14 |
---|---|
Python basic 4 - map(), import, zip() 한글을 입력하면 유니코드로 (0) | 2023.04.12 |
[Python] 한글 유니코드 2 - 한글을 byte 데이터로 만들기, byte(), bytearray() (0) | 2023.04.05 |
Python basic 2 - 수학 함수(all, max, min, sorted, pow)format, zip (0) | 2023.03.31 |
command 명령어 1 (0) | 2023.03.22 |