์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/41

 

[python ๊ธฐ์ดˆ] 9. ๋ชจ๋“ˆ๊ณผ ํŒจํ‚ค์ง€

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/40 https://silvercoding.tistory.com/39 https://silvercoding.tistory.com/38 https://silvercoding.tistory.com/37..

silvercoding.tistory.com

 

 

<์ฐธ๊ณ > 

https://docs.python.org/3.7/library/functions.html#open

 

Built-in Functions — Python 3.7.11 documentation

Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order. abs(x) Return the absolute value of a number. The argument may be an integer or a floating po

docs.python.org

 

 

 

* ์ฝ๊ธฐ๋ชจ๋“œ : r / ์“ฐ๊ธฐ๋ชจ๋“œ (๊ธฐ์กด ํŒŒ์ผ ์‚ญ์ œ) : w / ์ถ”๊ฐ€๋ชจ๋“œ (ํŒŒ์ผ ์ƒ์„ฑ ๋˜๋Š” ์ถ”๊ฐ€ ) 

* ๋ณธ ํฌ์ŠคํŒ…์˜ txt ํŒŒ์ผ : ํŒจ์ŠคํŠธ ์บ ํผ์Šค ์ œ๊ณต (resource - *.txt) 

 

 

 

1. ํŒŒ์ผ ์ฝ๊ธฐ 

- ์˜ˆ์ œ 1 

f = open('./resource/review.txt', 'r')
content = f.read()
print(content)
print(dir(f))
# ๋ฐ˜๋“œ์‹œ close ๋ฆฌ์†Œ์Šค ๋ฐ˜ํ™˜ 
f.close()

The film, projected in the form of animation, 
imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, 
which eventually paves the path for gaining a fresh perspective on an age-old problem. 
The story also happens to centre around two parallel characters, Shundi King and Hundi King, 
who are twins, but they constantly fight over unresolved issues planted in their minds 
by external forces from within their very own units. 

 ['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', 
'__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines'] 

open์˜ ์ธ์ž์— 'r'์„ ์ž‘์„ฑํ•˜์—ฌ ์ฝ๊ธฐ๋ชจ๋“œ๋กœ txtํŒŒ์ผ์„ ๊ฐ€์ ธ ์˜จ๋‹ค. dir์„ ์ถœ๋ ฅํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 2 : close ์ƒ๋žต ๊ฐ€๋Šฅ 

with open('./resource/review.txt', 'r') as f:
    c = f.read()
    print(c)
    print(list(c))
    print(iter(c))

The film, projected in the form of animation,
imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues,
which eventually paves the path for gaining a fresh perspective on an age-old problem.
The story also happens to centre around two parallel characters, Shundi King and Hundi King,
who are twins, but they constantly fight over unresolved issues planted in their minds
by external forces from within their very own units.
['T', 'h', 'e', ' ', 'f', 'i', 'l', 'm', ',', ' ', 'p', 'r', 'o', 'j', 'e', 'c', 't', 'e', 'd', ' ', 'i', 'n', ' ', 't', 'h', 'e', ' ', 'f', 'o', 'r', 'm', ' ', 'o', 'f', ' ', 'a', 'n', 'i', 'm', 'a', 't', 'i', 'o', 'n', ',', '\n', 'i', 'm', 'p', 'a', 
'r', 't', 's', ' ', 't', 'h', 'e', ' ', 'l', 'e', 's', 's', 'o', 'n', ' ', 'o', 'f', ' ', 'h', 'o', 'w', ' ', 'w', 'a', 'r', 's', ' ', 'c', 'a', 'n', ' ', 'b', 'e', ' ', 'e', 'l', 'u', 'd', 'e', 'd', ' ', 't', 'h', 'r', 'o', 'u', 'g', 'h', ' ', 'r', 'e', 'a', 's', 'o', 'n', 'i', 'n', 'g', ' ', 'a', 'n', 'd', ' ', 'p', 'e', 'a', 'c', 'e', 'f', 'u', 'l', ' ', 'd', 'i', 'a', 'l', 'o', 'g', 'u', 'e', 's', ',', '\n', 'w', 'h', 'i', 'c', 'h', ' ', 'e', 'v', 'e', 'n', 't', 'u', 'a', 'l', 'l', 'y', ' ', 'p', 'a', 'v', 'e', 's', ' ', 't', 'h', 'e', ' ', 'p', 'a', 't', 'h', ' ', 'f', 'o', 'r', ' ', 'g', 'a', 'i', 'n', 'i', 'n', 'g', ' ', 'a', ' ', 'f', 'r', 'e', 's', 'h', ' ', 'p', 'e', 'r', 's', 'p', 'e', 'c', 't', 'i', 'v', 'e', ' ', 'o', 'n', ' ', 'a', 
'n', ' ', 'a', 'g', 'e', '-', 'o', 'l', 'd', ' ', 'p', 'r', 'o', 'b', 'l', 'e', 'm', '.', '\n', 'T', 'h', 'e', ' ', 's', 't', 'o', 'r', 'y', ' ', 'a', 'l', 's', 'o', ' 
', 'h', 'a', 'p', 'p', 'e', 'n', 's', ' ', 't', 'o', ' ', 'c', 'e', 'n', 't', 'r', 'e', ' ', 'a', 'r', 'o', 'u', 'n', 'd', ' ', 't', 'w', 'o', ' ', 'p', 'a', 'r', 'a', 
'l', 'l', 'e', 'l', ' ', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's', ',', ' ', 'S', 'h', 'u', 'n', 'd', 'i', ' ', 'K', 'i', 'n', 'g', ' ', 'a', 'n', 'd', ' ', 'H', 'u', 'n', 'd', 'i', ' ', 'K', 'i', 'n', 'g', ',', '\n', 'w', 'h', 'o', ' ', 'a', 'r', 'e', ' ', 't', 'w', 'i', 'n', 's', ',', ' ', 'b', 'u', 't', ' ', 't', 'h', 'e', 
'y', ' ', 'c', 'o', 'n', 's', 't', 'a', 'n', 't', 'l', 'y', ' ', 'f', 'i', 'g', 'h', 't', ' ', 'o', 'v', 'e', 'r', ' ', 'u', 'n', 'r', 'e', 's', 'o', 'l', 'v', 'e', 'd', ' ', 'i', 's', 's', 'u', 'e', 's', ' ', 'p', 'l', 'a', 'n', 't', 'e', 'd', ' ', 'i', 'n', ' ', 't', 'h', 'e', 'i', 'r', ' ', 'm', 'i', 'n', 'd', 's', '\n', 'b', 'y', 
' ', 'e', 'x', 't', 'e', 'r', 'n', 'a', 'l', ' ', 'f', 'o', 'r', 'c', 'e', 's', ' ', 'f', 'r', 'o', 'm', ' ', 'w', 'i', 't', 'h', 'i', 'n', ' ', 't', 'h', 'e', 'i', 'r', ' ', 'v', 'e', 'r', 'y', ' ', 'o', 'w', 'n', ' ', 'u', 'n', 'i', 't', 's', '.']   
 <str_iterator object at 0x00000230D67D2160> 

์œ„์™€ ๊ฐ™์ด with๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด close๋ฅผ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค. with๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด close๋ฅผ ๊ผญ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง๊ธฐ. 

 

 

- ์˜ˆ์ œ 3 

with open('./resource/review.txt', 'r') as f:
    for c in f:
        print(c.strip())

 The film, projected in the form of animation, 
 imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, 
 which eventually paves the path for gaining a fresh perspective on an age-old problem. 
 The story also happens to centre around two parallel characters, Shundi King and Hundi King, 
 who are twins, but they constantly fight over unresolved issues planted in their minds 
 by external forces from within their very own units. 

for๋ฌธ์œผ๋กœ ์ถœ๋ ฅํ•ด๋ณผ ์ˆ˜ ๋„ ์žˆ๋‹ค. stripํ•จ์ˆ˜๋กœ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ ๋’ค ์ถœ๋ ฅํ•ด ์ค€๋‹ค. 

 

 

- ์˜ˆ์ œ 4 

with open('./resource/review.txt', 'r') as f:
    content = f.read()
    print('>>>', content)
    content = f.read()  # ๋‚ด์šฉ ์—†์Œ 
    print('>>>', content)

 >>> The film, projected in the form of animation, 
 imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, 
 which eventually paves the path for gaining a fresh perspective on an age-old problem. 
 The story also happens to centre around two parallel characters, Shundi King and Hundi King, 
 who are twins, but they constantly fight over unresolved issues planted in their minds 
 by external forces from within their very own units. 
 >>> 

์ด๋ ‡๊ฒŒ ๋ชจ๋“  line์„ ์ถœ๋ ฅํ•˜๊ณ  ๋‚˜๋ฉด, ์ค‘๋ณตํ•ด์„œ line์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†๋‹ค. ์ด๋ฏธ ์ปค์„œ๊ฐ€ ๋๊นŒ์ง€ ๊ฐ”๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

 

- ์˜ˆ์ œ 5 : readline() 

with open('./resource/review.txt', 'r') as f:
    line = f.readline()
    # print(line)
    while line:
        print(line, end='####')
        line = f.readline()

The film, projected in the form of animation,
####imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues,
####which eventually paves the path for gaining a fresh perspective on an age-old problem.
####The story also happens to centre around two parallel characters, Shundi King and Hundi King,
####who are twins, but they constantly fight over unresolved issues planted in their minds
####by external forces from within their very own units.####

readline()์€ ํ•œ์ค„์”ฉ ๋ฐ˜ํ™˜ ํ•ด ์ค€๋‹ค. 

 

 

- ์˜ˆ์ œ 6 : readlines() 

with open('./resource/review.txt', 'r') as f:
    contents = f.readlines()
    print(contents)
    for c in contents:
        print(c, end=" ****** ")

['The film, projected in the form of animation,\n', 'imparts the lesson of how wars 
can be eluded through reasoning and peaceful dialogues,\n', 'which eventually paves 
the path for gaining a fresh perspective on an age-old problem.\n', 'The story also 
happens to centre around two parallel characters, Shundi King and Hundi King,\n', 'who are twins, but they constantly fight over unresolved issues planted in their minds\n', 'by external forces from within their very own units.']
The film, projected in the form of animation,
 ****** imparts the lesson of how wars can be eluded through reasoning and peaceful 
dialogues,
 ****** which eventually paves the path for gaining a fresh perspective on an age-old problem.
 ****** The story also happens to centre around two parallel characters, Shundi King and Hundi King,
 ****** who are twins, but they constantly fight over unresolved issues planted in their minds
 ****** by external forces from within their very own units. ******

readlines๋Š” ๋ชจ๋“  ๋ฌธ์žฅ์„ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค€๋‹ค. 

 

 

- ์˜ˆ์ œ 7 

score = []

with open('./resource/score.txt', 'r') as f:
    for line in f:
        score.append(int(line))
    print(score)
    
print('Average : {:6.3}'.format(sum(score)/len(score)))

 [95, 78, 92, 89, 100, 66] 
 Average :   86.7 

ํ˜•๋ณ€ํ™˜์„ ํ•˜๊ณ , ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ด์„œ ํ‰๊ท ์„ ๊ตฌํ•ด์ฃผ๋Š” ์˜ˆ์ œ์ด๋‹ค. 

 

 

 

 

 

 

 

 

 

2. ํŒŒ์ผ ์“ฐ๊ธฐ 

- ์˜ˆ์ œ 1 

with open('./resource/text1.txt', 'w') as f:
    f.write("Nicegirl!\n")

open์˜ ์ธ์ž์— 'w'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์ƒ์„ฑ ๋ฐ ๊ธ€์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 2 

with open('./resource/text1.txt', 'a') as f:
    f.write("Goodgirl!")

open์˜ ์ธ์ž์— 'a'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€์”จ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 3 

from random import randint
with open('./resource/text2.txt', 'w') as f:
    for cnt in range(6):
        f.write(str(randint(1, 50)))
        f.write('\n')

for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ 6๊ฐœ์˜ ๋žœ๋ค ์ˆซ์ž๋ฅผ ๊ฐ ์ค„์— ํ•œ๊ฐœ์”ฉ ์ž‘์„ฑํ•ด ์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค. 

 

 

- ์˜ˆ์ œ 4 >>> writelines : ๋ฆฌ์ŠคํŠธ -> txt ํŒŒ์ผ๋กœ ์ €์žฅ 

# writelines : ๋ฆฌ์ŠคํŠธ -> ํŒŒ์ผ๋กœ ์ €์žฅ
with open('./resource/text3.txt', 'w') as f:
    list = ['Kim\n', 'Park\n', 'Cho\n']
    f.writelines(list)

writelines๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 5 : ํ”„๋ฆฐํŠธ ํ•จ์ˆ˜๋กœ ํŒŒ์ผ ์ƒ์„ฑ 

with open('./resource/text4.txt', 'w') as f:
    print('Test Contents1!', file=f)
    print('Test Contents2!', file=f)

printํ•จ์ˆ˜์˜ ์ธ์ž์— file=f ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ํŒŒ์ผ๋กœ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

+ Recent posts