Kyrol
Kyrol

Reputation: 3607

Error on processing files in a directory

I have a directory with images that I have to check. My code is

import imghdr as ih
import os

path = 'path_to_dir'

def check_format(path):
    for file in os.listdir(path):
        format = ih.what(file)
        print(format)
        if format != 'jpeg' and format != 'png':
            print("format error...\n")
            return -1
    return 0

I execute and I have this error:

... line 14, in what f = open(file, 'rb') IOError: [Errno 2] No such file or directory: 'world_cup.jpg'

but the file world_cup.jpg is a file in the directory.

Upvotes: 0

Views: 113

Answers (1)

Tomasz Lewowski
Tomasz Lewowski

Reputation: 1965

Change:

for file in os.listdir(path):
    format = ih.what(file)

into:

for file in os.listdir(path):
    format = ih.what(os.path.join(path, file))

Upvotes: 2

Related Questions