运行 Python 中的代码时,我一直遇到此 NOT ADirectory 错误

I have been experiencing this NOT ADirectory Error while running a code in Python

错误

Traceback (most recent call last):
  File "images.py", line 11, in <module>
    for filename in os.listdir(path):
NotADirectoryError: [Errno 20] Not a directory: 'images.py'

我正在使用这段代码,但上面的错误不断出现,不知道下一步该怎么做?

这是我的代码

import sys
import os
from PIL import Image

image_folder = sys.argv[0]
output_folder = sys.argv[1]

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

for filename in os.listdir(image_folder):
  img= Image.open(f'{image_folder}{filename}')
  img.save(f'{output_folder}{filename}','png')
  print('alldone!')

脚本参数的第一个元素 sys.argv[0] 是执行文件的路径 - images.py 在您的例子中。

变化:

image_folder = sys.argv[0]
output_folder = sys.argv[1]

收件人:

image_folder = sys.argv[1]
output_folder = sys.argv[2]