Purpose
이 Code Snippet은 'File의 입출력을 조작'하고자 합니다.
Dependency Packages
Nothing
Code Example
Ex1) open
과 close
로, 현재 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)
'Python Code Snippet' 카테고리의 다른 글
[Python Code Snippet] 파이토치 기본설정 (0) | 2021.03.24 |
---|---|
[Python Code Snippet] 코랩에 구글 드라이브 연결 (0) | 2021.03.24 |
[Python Code Snippet] 파일 확인 및 조작 (0) | 2021.03.23 |
[Python Code Snippet] 파일의 압축 해제 (0) | 2021.03.23 |
[Python Code Snippet] 조건부 파일 검색 (0) | 2021.03.23 |