일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- mysql
- ngui
- 배열
- SVN
- List
- conda
- Unity
- Shader
- C++
- SSH
- Eclipse
- pi
- 쉘
- sql
- awk
- expect
- with
- autovacuum
- boost
- if
- c++11
- FIND
- sqlite
- null
- dataframe
- python
- DROP
- ubuntu
- postgresql
- pandas
Archives
- Today
- Total
その先にあるもの…
[PYTHON] with ~ as 본문
with 블록
파일을 다룰 때 with 블록을 통해 명시적으로 close() 메소드를 호출하지 않고도 파일을 닫을 수 있습니다.
파일 객체는 내부적으로 __enter__(), __exit__() 가 구현이 되어 있습니다.
with 블록이 자동으로 블록을 종료할 때 __exit__() 메소드를 호출하며 파일을 close 시키게 됩니다.
file = open('text.txt')
print( file.readline(), end='')
file.close()
위의 코드를 with ... as로 바꾸면 다음과 같다.
with open('text.txt') as file :
print(file .readline(), end='')
'프로그래밍 > Python' 카테고리의 다른 글
[PYTHON] 라이브러리 설치 (0) | 2019.08.01 |
---|---|
[PYTHON] Anaconda (0) | 2019.08.01 |
NotePad++에서 Python 실행하기 (0) | 2019.03.21 |
[PYTHON] 파일 오픈할 때 ERROR CP949~ (0) | 2019.03.18 |
[PYTHON] Qt Designer 시그널/슬록 편집기 (0) | 2017.07.26 |
Comments