1

The ERROR

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'

I m using this code but the above error keeps coming, don't know what to do next?

This is my code

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!')
halfer
  • 19,824
  • 17
  • 99
  • 186
Rishabh Jain
  • 15
  • 1
  • 4
  • Please copy/paste exactly how you run this from the command line. The error lies in `image_folder = sys.argv[0]`. Print out `image_folder` and `output_folder` in your script to see what I mean – inspectorG4dget Jun 01 '20 at 06:10
  • The first element in `sys.argv` (`sys.argv[0]`) is the path of the script, not a path of a directory – בנימין כהן Jun 01 '20 at 06:12
  • ``sys.argv[1]`` get's the first command line argument where as ``sys.argv[0]`` get's the file name. – sushanth Jun 01 '20 at 06:12
  • inspectorG4dget i tried but it says Traceback (most recent call last): File "images.py", line 6, in output_folder = sys.argv[2] IndexError: list index out of range – Rishabh Jain Jun 01 '20 at 06:25

1 Answers1

0

The first element of your script arguments, sys.argv[0], is the path of the file executed - images.py in your case.

Change:

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

To:

image_folder = sys.argv[1]
output_folder = sys.argv[2]
Gabio
  • 9,126
  • 3
  • 12
  • 32