๋ชฉ์ฐจ
์ง๊ธ๊น์ง๋ ๊ฐ์ ์ ์ถ๋ ฅํ๋ ๊ฒ์ ์ง์ ์ ๋ ฅํ๊ณ ๋ฐ๋ก ์ถ๋ ฅํ๋ ๋ฐฉ์์ด์๋ค.
ํ์ง๋ง ํ์ผ์ ์ด์ฉํด ์ ์ถ๋ ฅํ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค!
์ฌ์ฉ๋ฒ)
ํ์ผ ๊ฐ์ฒด = open("ํ์ผ๋ช
.txt",'w',encoding='utf8')
ํ์ผ์ ์์ฑํ๊ธฐ ์ํด์ openํจ์๋ฅผ ์ฌ์ฉํ๊ณ ํ์ผ๋ช , ํ์ผ์ด๊ธฐ๋ชจ๋๋ฅผ ์ ๋ ฅ๊ฐ์ผ๋ก ๋ฐ๋๋ค.
์ธ์ฝ๋ฉ ๋ฐฉ์์ ์ง์ ํด์ฃผ๋ฉด ํ๊ธ์ ๋ํ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์๋๋ค๊ณ ํ๋ค.
์ด๋ ํ์ผ ์ด๊ธฐ๋ชจ๋๋ 3๊ฐ์ง๊ฐ ์๋ค.
w = ์ฐ๊ธฐ๋ชจ๋ / ํ์ผ์ ๋ด์ฉ์ ์ธ ๋ ์ฌ์ฉ, ๊ธฐ์กด ๋ด์ฉ์ ๋ชจ๋ ์ญ์ ํ๊ณ ์๋กญ๊ฒ ๊ธฐ๋ก
a = ์ด์ด์ ์ฐ๊ธฐ๋ชจ๋ / ํ์ผ์ ๋ด์ฉ์ ์ธ ๋ ๊ธฐ์กด ๋ด์ฉ์ ๋ณด์กดํ๊ณ ๋ค์ ์ด์ด์ ์ธ ๋ ์ฌ์ฉ
r = ์ฝ๊ธฐ๋ชจ๋ / ํ์ผ์ ์ฝ๊ธฐ๋ง ํ ๋ ์ฌ์ฉ
ํ์ผ ๊ฐ์ฒด = open("C:/Workspace/ํ์ผ๋ช
.txt",'w',encoding='utf8')
์์ฒ๋ผ ํ์ผ์ ์์น๋ฅผ ์ง์ ํด์ค ์๋ ์๋ค.(์ญ์ฌ๋์๋ ์ฌ์ฉ๊ฐ๋ฅ but, 2๊ฐ์ฉ ๋ถ์ฌ์ ์ฐ๊ธฐ)
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
1. w
์ฐ๊ธฐ๋ชจ๋๋ก ํ์ผ ์ด๊ธฐ
f = open("score.txt","w",encoding="utf8")
print("์ํ : 80", file=f)
print("์์ด : 70", file=f)
f.close()
ํ์ผ์ ์ด์์ผ๋ฉด close()๋ฅผ ์ด์ฉํด ์ด๋ ค์๋ ํ์ผ์ ์ง์ ๋ซ์์ฃผ๋ ๊ฒ์ด ์ข๋ค.
์ด์ ๋ ์ฐ๊ธฐ๋ชจ๋๋ก ์ด์๋ ํ์ผ์ ๋ซ์ง ์๊ณ ๋ค์ ์ฌ์ฉํ๋ ค๊ณ ํ๋ค๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ด๋ค.
๋ค์๊ณผ ๊ฐ์ด ํ์ผ์ด ์์ฑ๋๋ ๊ฒ์ ์ ์ ์๋ค.
f = open("score.txt","w",encoding="utf8")
print("์ํ : 100", file=f)
print("์์ด : 95", file=f)
f.close()
์์ฒ๋ผ ํ์ผ ๋ด์ฉ๋ง ๋ฐ๊พธ๊ณ ์ถ๋ ฅ์ ํ๋ฉด
ํ์ผ ๋ด์ฉ์ '๋ฎ์ด์ฐ๊ธฐ'ํ๋ ๊ฒ์ ์ ์ ์๋ค.
ํ์ผ์ ์ ๋ ฅ์ ํ ๋๋ writeํจ์๋ฅผ ํ์ฉํ ์ ์๋ค.
f = open("file.txt","w",encoding="utf8")
for i in range(1,6):
data = "{0}๋ฒ์งธ ์ค์
๋๋ค.\n".format(i)
f.write(data)
f.close()
์์ฒ๋ผ print ๋์ write๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์จ๋ผ ์ ์๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
2. a
์ด์ด์ ์ฐ๊ธฐ๋ชจ๋๋ก ํ์ผ ์ด๊ธฐ
f = open("score.txt","a",encoding="utf8")
print("๊ตญ์ด : 80", file=f)
print("๊ณผํ : 95", file=f)
f.close()
์ด์ด์์ฐ๊ธฐ๋ชจ๋๋ฅผ ์ด์ฉํ๊ธฐ ๋๋ฌธ์ ํ์ผ์ด ์ด๊ธฐํ ๋์ง ์๊ณ ์ด์ด์
์จ์ง ๊ฒ์ ์ ์ ์๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
3. r
ํ์ผ์ ๋ด์ฉ ์ฝ์ด์ค๊ธฐ
1) read
f = open("file.txt","r",encoding="utf8")
data = f.read()
print(data)
f.close()
ํ์ผ์ ๋ด์ฉ ์ ์ฒด๋ฅผ ๋ฌธ์์ด๋ก ๋ฐํํด์ค๋ค.
2) readline
f = open("file.txt","r",encoding="utf8")
data = f.readline()
print(data)
f.close()
readlineํจ์๋ ํ์ผ์ ํ ์ค๋ง ์ฝ์ด์จ๋ค.
(โป ์ฃผ์ํ ์ : readline์ \n์ด ์ค๋์ ๋ด์ฅ๋์ด ์๋ค!! ์ง์ฐ๋ ๋ฐฉ๋ฒ์ end ํน์ strip์ ํ์ฉํ์!)
f = open("file.txt","r",encoding="utf8")
while True:
data = f.readline()
if not data:
break
print(data, end="")
f.close()
#readlineํ ๋๋ง๋ค \n์ด ํฌํจ๋์ด ์์ด์ ๋ฌธ์ฅ ๋ง์ง๋ง์ ""์ผ๋ก ์ง์
์ด๋ ๊ฒ ํ์ผ์ ์ค ์๋ฅผ ๋ชจ๋ฅผ ๋๋ ๋ฐ๋ณต๋ฌธ์ ์ด์ฉํ๋ฉด ๋๋ค.
3) readlines
f = open("file.txt","r",encoding="utf8")
datalist = f.readlines()
for data in datalist:
data = data.strip()
print(data)
f.close()
#strip()ํจ์๋ฅผ ํ์ฉํ๋ฉด ์ค ๋์ ๋ด์ฅ๋ \n์ ์ ๊ฑฐํ ์ ์๋ค.
readlinesํจ์๋ ๊ฐ๊ฐ์ ์ค์ ์์๋ก ํ๋ ๋ฆฌ์คํธ๋ก ๋ฐํํ๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
with๋ฌธ ํ์ฉํ๊ธฐ
with๋ฅผ ์ด์ฉํ๋ฉด ์กฐ๊ธ ๋ ํธ๋ฆฌํ๊ฒ ํ์ผ์ ์ด๊ณ ๋ซ์ ์ ์๋ค.
์ฌ์ฉ๋ฒ)
with open("ํ์ผ๋ช
.txt",'w') as ํ์ผ๊ฐ์ฒด:
์์ ๊ฐ์ด ์ฐ๋ฉด ํ์ผ์ ์ง์ ๋ซ์ง ์์๋ ๋๋ค.
ํ์ผ๊ฐ์ฒด๊ฐ ์๋์ผ๋ก close๋๋ค๋ ๊ฒ์ด๋ค!
'๐ | Python > ํ์ด์ฌ ๋ฌธ๋ฒ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํจ์์ ๋ฉ์๋ (0) | 2021.10.02 |
---|---|
pickle (0) | 2021.09.28 |
๋ณ์์ ์ ํจ๋ฒ์ (0) | 2021.09.22 |
ํจ์_(2) (0) | 2021.09.20 |
ํจ์_(1) (0) | 2021.09.19 |
๋๊ธ