Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 데이터 분석
- 파이썬
- 머신러닝
- 프로그래머스
- 튜닝
- python3
- matplotlib
- SQL
- sklearn
- R
- level 2
- Python
- 빅분기
- 카카오
- Numpy
- seaborn
- oracle
- level 1
- 실습
- Kaggle
- 실기
- 오라클
- 빅데이터 분석 기사
- pandas
- Oracel
- 알고리즘
- 코딩테스트
Archives
- Today
- Total
라일락 꽃이 피는 날
[Python] fileopen 본문
728x90
파일 입출력
터미널에 결과물을 출력하는 것이 아닌 파일 형태로 출력 할 수 있게 하기 위해 사용한다.
터미널에서 input 함수를 사용하여 입력를 받아 동작하는 것이 아닌 파일 형태로 입력을 받아서 동작 할 수 있게 하기 위해 사용한다.
open('file_path/file_name', mode)
mode : 읽기(r), 덮어쓰기(w), 이어쓰기(a)
path = 'C:/test.txt'
fw = open(path, mode='w', encoding='utf-8')
fw.write('쓰기 위한 문자열 1\n')
fw.write('쓰기 위한 문자열 2\n')
fw.close()
path = 'C:/test.txt'
# 예외 처리
try:
fo = open(path, mode='r')
print(fo.read(), 'CP949로 읽기 완료')
fo.close()
except FileNotFoundError:
fw = open(path, mode='w', encoding='utf-8')
fw.write('예외가 발생 되어 쓰기 동작\n')
fw.close()
except UnicodeDecodeError:
fo = open(path, mode='r', encoding='utf-8')
print(fo.read(), '유니코드로 읽기 완료')
fo.close()
728x90
'프로그래밍 > Python' 카테고리의 다른 글
[Python] Class (클래스) (0) | 2021.08.16 |
---|---|
[Python] Package, Module (0) | 2021.06.15 |
[Python] strftime, strptime (0) | 2021.06.14 |
[Python] Comprehension (함축) (0) | 2021.06.14 |
[Python] Packing, Unpacking (0) | 2021.06.14 |