코딩, 개발에 대한 기록 저장소

파이썬(Python) 텍스트 파일 라인별 읽는 방법

파이썬(Python) 텍스트 파일 라인별 읽는 방법

읽을 파일이 다음과 같은 경우:

data.txt

a
b
c
d

readlines()

path = "data.txt"

with open(path) as f:
    lines = f.readlines()

print(lines)

결과:

['a\n', 'b\n', 'c\n', 'd']

splitlines()

path = "data.txt"

with open(path) as f:
    lines = f.read().splitlines()

print(lines)

결과:

['a', 'b', 'c', 'd']