파이썬 sys.argv 의미

개발자 강세영·2022년 7월 5일
0

TIL

목록 보기
42/65
# pillow.py
import os, sys

from PIL import Image

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".resized.jpg"

    if infile != outfile:
        try:
            with Image.open(infile) as im:
                im.resize((500,700), reducing_gap=True).save(outfile, "JPEG")

        except OSError:
            print("cannot resize for", infile)

파이썬의 PILLOW 라이브러리를 사용하여 이미지 파일을 리사이즈하는 코드이다.
sys.argv란 파이썬 파일을 실행할때 실행파일 뒤에 넣는 것들을 인자로 받는다는 의미이다.
터미널에서 python pillow.py test.jpg을 실행하면서 sys.argv[0]의 값을 확인해보면 실행되는 파이썬 파일인 pillow.py이 나온다.
sys.argv[1]은 test.jpg 이다. 정상적으로 실행된 경우 test.jpg가 코드에 쓰여진 대로 리사이즈된 파일이 생성된다.

0개의 댓글