Home > Blockchain >  Contains more or equal to 2 .jpg files
Contains more or equal to 2 .jpg files

Time:04-10

I'm very new to python, just trying to figure it all out but here's my newbie question,

If I have a folder and inside, for each folder, I need to see if it contains more or equal to 2 .jpg files

CodePudding user response:

Try something like this:

import glob

files = glob.glob("C:/folder/*.jpg")
if len(files) >= 2:
    print("This folder contains two or more .jpg files")

CodePudding user response:

here is my solution:

from os import listdir
from os.path import isfile, join, splitext
def jpg_files():
    onlyfiles = [f for f in listdir("./") if isfile(join("./", f))]

    jpg_files_array = []
    for file in onlyfiles:
        ext = splitext(file)[1]
        if ( ext == ".jpg" ):
            jpg_files_array.append(file)
            continue
    return jpg_files_array
def length_jpg_files():
    files = jpg_files()
    length = len(files)
    return length
def more_or_two():
    if (length_jpg_files() == 2):
        print("Length of the .jpg files are 2")
    else :
        print("Length of the .jpg files are more than 2")
        
more_or_two()

jpg_files() returns the .jpg files in your dir

length_jpg_files() returns the length of .jpg files in your dir

more_or_two() prints there are more than two .jpg file or there are two .jpg files

Thanks, Sivayogeith

  • Related