Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 2

Neural Network in Python: IsADirectoryError: [Errno 21] Is a directory: 'input_data'

I am trying to load some JPGs into a GAN and I've already imported train_test_split. I have a folder of training/testing data on my desktop called 'input_data'. Inside the folder are two subfolders, Test and Train. Inside the Test and Train folders are Images1 and Images2.

I'm a Python beginner so I'd really appreciate some advice. What am I doing wrong here?

def loadImages(path):
    # return array of images
    imagesList = listdir(path)
    loadedImages = []
    for image in imagesList:
        img = PImage.open(path + image)
        loadedImages.append(img)
    return loadedImages
path = "input_data"
for filename in os.listdir(path):
   content = open(os.path.join(path, filename), mode='rb').read()
with open(path, 'rb') as fh:
# your images in an array
    imgs = loadImages(path)
def loadImages():
    (x_train, y_train), (x_test, y_test) = loadImages()
    x_train = (x_train.astype(np.float32) - 127.5)/127.5
    x_train = x_train.reshape(60000, 784)
    return (x_train, y_train, x_test, y_test)

 

The error:

Traceback (most recent call last):
  File "gan3.py", line 31, in <module>
    with open(path, 'rb') as fh:
IsADirectoryError: [Errno 21] Is a directory: 'input_data/Train'

 

1 Reply
Commander Lvl 2
Message 2 of 2

Re: Neural Network in Python: IsADirectoryError: [Errno 21] Is a directory: 'input_data'

os.listdir() returns all files _and_ folders. You cannot call os.open() on a folder. You will want to call os.isfile() or os.isdir() before calling os.open().

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!