■ _TemporaryFileWrapper 클래스의 flush 메소드를 사용해 버퍼를 비우는 방법을 보여준다.
※ with 구문을 벗어날 때 임시 파일이 삭제되기 때문에 flush 메소드를 사용할 일을 보통 없다.
※ 하지만 with 구문 내에서 임시 파일을 작성하고 해당 파일을 다시 읽어서 처리를 할 경우 flush 메소드를 호출해야 파일을 읽을 수 있다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import tempfile sourceString = """ "Team" , "Payroll (millions)", "Wins" "Nationals", 81.34 , 98 "Reds" , 82.20 , 97 "Yankees" , 197.96 , 95 "Giants" , 117.62 , 94 """.strip() with tempfile.NamedTemporaryFile(delete = True, mode = "w+") as temporaryFileWrapper: temporaryFileWrapper.write(sourceString) temporaryFileWrapper.flush() |