Featured image of post 'Python 코드 조각'

'Python 코드 조각'

표준 라이브러리를 사용한 샘플 코드 소개입니다.

이미지 다운로드 및 표시하기

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import urllib.request
import tempfile
import os
import webbrowser
import time

url = "https://www.aomori-ringo.or.jp/kids/wp-content/uploads/2021/11/apple.png"

try:
    with urllib.request.urlopen(url) as response:
        img_data = response.read()

    # 임시 파일에 저장하여 표시
    with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
        tmp.write(img_data)
        print(f"file://{tmp.name}")
        webbrowser.open(f"file://{tmp.name}")
        time.sleep(3)
except Exception as e:
    print(f"에러가 발생했습니다: {e}")

finally:
    if 'tmp' in locals():
        os.unlink(tmp.name)  # 임시 파일 삭제
comments powered by Disqus
Hugo로 만듦
JimmyStack 테마 사용 중