Coding/Code in the Morning

211101_Code in the morning

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 11. 1. 10:45

04์žฅ ์—ฐ์Šต๋ฌธ์ œ

Q5

๋‹ค์Œ์€ "test.txt"๋ผ๋Š” ํŒŒ์ผ์— "Life is too short" ๋ฌธ์ž์—ด์„ ์ €์žฅํ•œ ํ›„ ๋‹ค์‹œ ๊ทธ ํŒŒ์ผ์„ ์ฝ์–ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค.

f1 = open("test.txt", 'w')
f1.write("Life is too short")

f2 = open("test.txt", 'r')
print(f2.read())

์ด ํ”„๋กœ๊ทธ๋žจ์€ ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ƒํ•œ "Life is too short"๋ผ๋Š” ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ƒํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด ๋ณด์ž.

 

ํ’€์ด: ๋‹ซ์•„์ค˜์•ผ! 

๋”๋ณด๊ธฐ
f1 = open("test.txt", 'w')
f1.write("Life is too short")
f1.close()

f2 = open("test.txt", 'r')
print(f2.read())
f2.close()

Q6

์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ํŒŒ์ผ(test.txt)์— ์ €์žฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด์ž. (๋‹จ ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋”๋ผ๋„ ๊ธฐ์กด์— ์ž‘์„ฑํ•œ ๋‚ด์šฉ์„ ์œ ์ง€ํ•˜๊ณ  ์ƒˆ๋กœ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.)

 

ํ’€์ด: 

๋”๋ณด๊ธฐ
user_input = input("์ €์žฅํ•  ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”: ")
f = open('test.txt','a')
f.write(user_input)
f.write("\n")
f.close()

 

Q7

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ์ง€๋‹Œ ํŒŒ์ผ test.txt๊ฐ€ ์žˆ๋‹ค. ์ด ํŒŒ์ผ์˜ ๋‚ด์šฉ ์ค‘ "java"๋ผ๋Š” ๋ฌธ์ž์—ด์„ "python"์œผ๋กœ ๋ฐ”๊พธ์–ด์„œ ์ €์žฅํ•ด ๋ณด์ž.

Life is too short you need java

โ€ป replace ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž.

๋”๋ณด๊ธฐ
f = open('test.txt','r')
body = f.read()
f.close()

body = body.replace('java','python')

f = open('test.txt','w')
f.write(body)
f.close()

 

'Coding > Code in the Morning' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

211123_Code int the morning  (0) 2021.11.23
211103_Code in the morning  (0) 2021.11.03
211029_Code in the morning  (0) 2021.10.29
211027_Code in the morning  (0) 2021.10.27
211026_Code in the morning  (0) 2021.10.26