ex. 파일 시스템에 있는 SNS 프로필 사진을 관리하는 프로그램을 작성한다고 가정
1) get 메서드 사용
pictures = {}
path = 'profile_1234.png'
with open(path, 'wb') as f:
f.write(b'image data here 1234')
if (handle := pictures.get(path)) is None:
try:
handle = open(path, 'a+b')
except OSError:
print(f'Failed to open path {path}')
raise
else:
pictures[path] = handle
handle.seek(0)
image_data = handle.read()
print(pictures)
print(image_data)
>> {'profile_1234.png': <_io.BufferedRandom name='profile_1234.png'>}
b'image data here 1234'