Search code examples
pythonargvpython-os

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


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!')

Solution

  • 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]