-
[Python] with문에 대해 알아보자 (실행과 종료)Python 2021. 11. 24. 21:51
with statement:
__enter__ 와 __exit__ 특수메소드를 가진 객체에 with 문을 쓸 수 있다.
자동으로 파일에 접근하고 수행하고 닫아준다.
example:
with {expression} as {variable}:
실행
with open('textfile.txt', 'r') as file: contents = file.read() # 위 구문과 동일한 내용 file = open('textfile.txt', 'r') contents = file.read() file.close()
'Python' 카테고리의 다른 글
[Python] 지역변수들이 뭐가 있나 확인해보자 locals() (0) 2021.11.25 [Python] text를 index해보자 module : re (0) 2021.11.24 [Python] 예외 처리를 해보자 try, except, else, finally (0) 2021.11.24 [Python] list에서 객체를 빼오자 pop (0) 2021.11.24 [Python] split 이용해서 간지 나게 리스트 만들기 (0) 2021.11.23