[Python] Sys.argv list index out of range [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 6, 2025.

  1. Stack

    Stack Membro Participativo

    guys. Can someone explain to me why can't I run this script (for converting a jpg into png) in PyCharm? I get an error list index out of range. I have the code and the jpg files in the same directory. The same error occured when I ran a script to merge some pdf files (the files and the .py script were as well in the same directory).

    import sys
    import os
    from PIL import Image

    path = sys.argv[1]
    directory = sys.argv[2]

    if not os.path.exists(directory):
    os.makedirs(directory)

    for filename in os.listdir(path):
    clean_name = os.path.splitext(filename)[0]
    img = Image.open(f'{path}{filename}')
    img.save(f'{directory}/{clean_name}.png', 'png')
    print('all done!')

    Continue reading...

Compartilhe esta Página