๐Python
Welcome to Python : ํ์ผ ์ฝ๊ณ ์ฐ๊ธฐ
4:Bee
2023. 7. 13. 13:43
728x90
- ํจ์ : https://hi-code.tistory.com/119
- ์ฌ์ฉ์ ์ ์ถ๋ ฅ : https://hi-code.tistory.com/120
- ํ์ผ ์ฝ๊ณ ์ฐ๊ธฐ : https://hi-code.tistory.com/121
- ํ๋ก๊ทธ๋จ์ ์ ์ถ๋ ฅ : https://hi-code.tistory.com/122
Title | Code | Console |
ํ์ผ ์ฝ๊ณ ์ฐ๊ธฐ | ||
ํ์ผ ์์ฑํ๊ธฐ |
f = open("์ํ์ผ.txt", 'w') f.close
|
|
ํ์ผ ์ด๊ธฐ ๋ชจ๋ | r - ์ฝ๊ธฐ ๋ชจ๋ : ํ์ผ์ ์ฝ๊ธฐ๋ง ํ ๋ ์ฌ์ฉํ๋ค. w - ์ฐ๊ธฐ ๋ชจ๋ : ํ์ผ์ ๋ด์ฉ์ ์ธ ๋ ์ฌ์ฉํ๋ค. a - ์ถ๊ฐ ๋ชจ๋ : ํ์ผ์ ๋ง์ง๋ง์ ์๋ก์ด ๋ด์ฉ์ ์ถ๊ฐํ ๋ ์ฌ์ฉํ๋ค. |
|
ํ์ผ(w)์ ์ด์ด ๋ด์ฉ ์ฐ๊ธฐ |
f = open("์ํ์ผ.txt", 'w') for i in range(1, 11):
data = "%d๋ฒ์งธ ์ค์
๋๋ค.\n" % i
f.write(data)
f.close
|
|
readline ํจ์ |
f = open("์ํ์ผ.txt", 'r') line = f.readline()
print(line)
f.close()
|
1๋ฒ์งธ ์ค์ ๋๋ค. |
f = open("์ํ์ผ.txt", 'r') while True:
line = f.readline()
if not line:
break
print(line)
f.close()
|
1๋ฒ์งธ ์ค์
๋๋ค. 2๋ฒ์งธ ์ค์ ๋๋ค. 3๋ฒ์งธ ์ค์ ๋๋ค. . . . 10๋ฒ์งธ ์ค์ ๋๋ค. |
|
readline's'ํจ์ |
f = open("์ํ์ผ.txt", 'r') lines = f.readlines()
for line in lines:
print(line)
f.close
|
1๋ฒ์งธ ์ค์
๋๋ค. 2๋ฒ์งธ ์ค์ ๋๋ค. . . . 10๋ฒ์งธ ์ค์ ๋๋ค. |
.strip() |
f = open("์ํ์ผ.txt", 'r') lines = f.readlines()
for line in lines:
line = line.strip()
print(line)
f.close
|
1๋ฒ์งธ ์ค์
๋๋ค. . . . 8๋ฒ์งธ ์ค์ ๋๋ค. 9๋ฒ์งธ ์ค์ ๋๋ค. 10๋ฒ์งธ ์ค์ ๋๋ค. |
readํจ์ |
f = open("์ํ์ผ.txt", 'r') data = f.read()
print(data)
f.close
|
|
ํ์ผ ๊ฐ์ฒด for๋ฌธ |
f = open("์ํ์ผ.txt", 'r') for line in f:
print(line)
f.close
|
|
์๋ก์ด ๋ด์ฉ ์ถ๊ฐํ๊ธฐ |
f = open("์ํ์ผ.txt", 'a') for i in range(11, 20):
data = "%d๋ฒ์งธ ์ค์
๋๋ค.\n" % i
f.write(data)
f.close
|
'11๋ฒ์งธ ์ค์ ๋๋ค.'์์ '20๋ฒ์งธ ์ค์ ๋๋ค.'๊น์ง ์ถ๊ฐ๊ฐ ๋๋ค. |
with๋ฌธ ์ฌ์ฉํ๊ธฐ |
with open("foo.txt", "w") as f: f.write("Life is too short, you need python")
|
728x90