with ~ as 구문

CODEDRAGON Development/Python

반응형


 

 

with ~ as 구문

·       with구문은 파이썬 2.5부터 지원합니다.

·       with ~ as 구문을 사용시 파일을 오픈하고 명시적으로 close()함수를 호출해주지 않아도 with 블록을 벗어나는 순간 열린 파일 객체 f가 자동으로 close 되어집니다.

 

with open('C:/CodeLab/test06.txt') as f:

    print( f.readlines() )

    print( f.closed )

 

["Anything you're good at contributes to happiness.Bertrand Russell"]

False

 

 

 

 

with 구문을 빠져나간 후 f.closed 확인

print( f.closed )

 

True

 

 


반응형

'Development > Python' 카테고리의 다른 글

shutil 모듈 (Shell Utils)  (0) 2019.12.30
zip()  (0) 2019.12.29
파일 입출력 관련 함수들  (0) 2019.12.27
NumPy Reference, NumPy Manual  (0) 2019.12.26
리스트 슬라이싱(Slicing)  (0) 2019.12.25