[python] 파일 읽기 쓰기 replace

카초·2024년 5월 16일

텍스트 파일 열고 쓰기 및 replace 문을 좀 더 익숙하게 쓰고자 연습했다.

with open('anything.txt') as file:
    content = file.read()
print(content)

# hi -> hello 로 교체
re_content = content.replace('hi','hello')
print(re_content)


# 쓰기용으로 txt 파일 열고 교체본으로 덮어쓰기
with open('anything.txt', 'w') as file:
    file.write(re_content)
    if 1:
        print("good good")
    

실행결과

0개의 댓글