open function 설명
open(file_path, mode)
파라메터 | 설명 |
file_path | 파일이 저장되어있거나 저장될 경로 |
mode | r: read, w: write 모드, a: 파일 추가 모드 |
여기에서는 mode w로 사용해 보자
1. 파일 쓰기 기본
with open("temp.txt", "w") as wf:
wf.write("Welcome to the Python world")
이렇게 실행하면 python 이 실행되는 경로에 temp.txt 파일이 생기고
Welcome to the Python world
내용이 temp.txt 에 입력이 되어있다.
그러나 실제 사용할때는 이렇게 간단하지 않다.
많은양의 데이터를 써야할때는?
2. list 파일 쓰기
data_list = [
"Beautiful is better than ugly.",
"Explicit is better than implicit.",
"Simple is better than complex.",
"Complex is better than complicated.",
"Flat is better than nested.",
"Sparse is better than dense.",
"Readability counts.",
"Special cases aren't special enough to break the rules.",
"Although practicality beats purity.",
"Errors should never pass silently.",
"Unless explicitly silenced.",
"In the face of ambiguity, refuse the temptation to guess.",
"There should be one-- and preferably only one --obvious way to do it.",
"Although that way may not be obvious at first unless you're Dutch.",
"Now is better than never.",
"Although never is often better than *right* now.",
"If the implementation is hard to explain, it's a bad idea.",
"If the implementation is easy to explain, it may be a good idea.",
"Namespaces are one honking great idea -- let's do more of those!"
]
with open("temp.txt", "w") as wf:
wf.writelines(data_list)
temp.txt 파일을 열어보면
엔터가 안들어가 있다...
Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!
엔터를 넣으려면 아래 두가지 방법으로 바꿀 수 있다.
(이 외에 다른방법도 많다.)
data_list = [
"Beautiful is better than ugly.",
"Explicit is better than implicit.",
"Simple is better than complex.",
"Complex is better than complicated.",
"Flat is better than nested.",
"Sparse is better than dense.",
"Readability counts.",
"Special cases aren't special enough to break the rules.",
"Although practicality beats purity.",
"Errors should never pass silently.",
"Unless explicitly silenced.",
"In the face of ambiguity, refuse the temptation to guess.",
"There should be one-- and preferably only one --obvious way to do it.",
"Although that way may not be obvious at first unless you're Dutch.",
"Now is better than never.",
"Although never is often better than *right* now.",
"If the implementation is hard to explain, it's a bad idea.",
"If the implementation is easy to explain, it may be a good idea.",
"Namespaces are one honking great idea -- let's do more of those!"
]
with open("temp.txt", "w") as wf:
wf.write("\n".join(data_list))
data_list = [
"Beautiful is better than ugly.",
"Explicit is better than implicit.",
"Simple is better than complex.",
"Complex is better than complicated.",
"Flat is better than nested.",
"Sparse is better than dense.",
"Readability counts.",
"Special cases aren't special enough to break the rules.",
"Although practicality beats purity.",
"Errors should never pass silently.",
"Unless explicitly silenced.",
"In the face of ambiguity, refuse the temptation to guess.",
"There should be one-- and preferably only one --obvious way to do it.",
"Although that way may not be obvious at first unless you're Dutch.",
"Now is better than never.",
"Although never is often better than *right* now.",
"If the implementation is hard to explain, it's a bad idea.",
"If the implementation is easy to explain, it may be a good idea.",
"Namespaces are one honking great idea -- let's do more of those!"
]
with open("temp.txt", "w") as wf:
for data in data_list:
wf.write(data+"\n")
3. 파일 추가 모드 (mode : a)
mode : a 로 설정하면 기존 파일에 추가로 입력이 된다.
with open("temp.txt", "a") as wf:
for data in data_list:
wf.write("welcome to the Python")
'python > 중급' 카테고리의 다른 글
log 가 증식할때 (0) | 2021.09.26 |
---|---|
csv read & write (0) | 2021.09.05 |
list (append, extend) (0) | 2021.09.05 |
file read (0) | 2021.08.22 |
comprehension (0) | 2021.06.27 |