Purpose

이 Code Snippet은 'File의 입출력을 조작'하고자 합니다.


Dependency Packages

Nothing


Code Example

Ex1) openclose로, 현재 Directory 아래에 덮어쓰기로 File 조작

f = open('tmp.txt', 'w')
f.write('this is testing')
f.close()

 

Ex2) with block으로, 현재 Directory 아래에 이어쓰기로 File 조작

with open('tmp.txt', 'a') as f:
    f.write('testing again')

 

Ex3) File의 모든 Data를 List로 저장

f = open('tmp.txt', 'r')
lines = f.readlines()
f.close()

 

Ex4) File의 Data를 Line 기준으로 출력

with open(tmp.txt', 'r') as f :
    while True :
        line = f.readline()
        if not line :
            break
        print(line)

PS

File I/O에 관련된 자세한 정보는 여기를 참고바랍니다. (Link)

+ Recent posts