Python Forum
How To Modify This Code?? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How To Modify This Code?? (/thread-6350.html)



How To Modify This Code?? - digitalmatic7 - Nov-18-2017

for img_scan in os.listdir('./test_folder'):
    if img_scan.endswith('.jpg'):
        print(str(img_scan))
The code scans for .jpg images in the directory test_folder.

The problem is it outputs as img1.jpg img2.jpg and what I need instead is the number of images in the folder.

Also I'm not able to get it to scan for other image formats, it keeps saying single argument only.

Help please, been locked up on this issue for 3+ hours lol Rolleyes Smile


RE: How To Modify This Code?? - heiner55 - Nov-18-2017

It is your task to use a list for num1, num2, ...

import os
num1 = 0
num2 = 0
for img_scan in os.listdir('./test_folder'):
    if img_scan.endswith('.jpg'):
        num1 = num1 + 1
    if img_scan.endswith('.png'):
        num2 = num2 + 1
print("jpg:",num1)
print("png:",num2)



RE: How To Modify This Code?? - snippsat - Nov-18-2017

endswith() can take tuple as parameter.
Using os.scandir() is faster than listdir().
import os

with os.scandir('.') as it:
    count = 0
    for pic in it:
        if pic.name.endswith(('.jpg', '.png')):
            count += 1            
    print(count)
This will count all jpg  and png images in a folder.
If need a separate count,split it up like heiner55 show.


RE: How To Modify This Code?? - digitalmatic7 - Nov-18-2017

(Nov-18-2017, 08:06 AM)snippsat Wrote: endswith() can take tuple as parameter.
Using os.scandir() is faster than listdir().
import os

with os.scandir('.') as it:
    count = 0
    for pic in it:
        if pic.name.endswith(('.jpg', '.png')):
            count += 1            
    print(count)
This will count all jpg  and png images in a folder.
If need a separate count,split it up like heiner55 show.

You have no idea how good it feels to have that solution, it was driving me nuts haha. Thanks boss Thumbs Up


RE: How To Modify This Code?? - digitalmatic7 - Nov-18-2017

UPDATE

for img_scan in os.listdir('./' + str(folder_counter)):
    if img_scan.endswith(('.jpg', '.png', '.jpeg', '.gif')):
        global new_list
        new_list = [str(img_scan)]
        print(new_list)
This code prints a list with 5 items in it..

But if I take print on a new line outside of the (function?) it only gives me the last list item.

Also it's giving me error "Global variable is undefined at the module level"


RE: How To Modify This Code?? - snippsat - Nov-18-2017

You most define a list before the loop,then append to it.
import os

img_lst = []
for img_scan in os.listdir('.'):
    if img_scan.endswith(('.jpg', '.png', '.jpeg', '.gif')):
        img_lst.append(img_scan)

print(img_lst)



RE: How To Modify This Code?? - digitalmatic7 - Nov-18-2017

(Nov-18-2017, 04:27 PM)snippsat Wrote: You most define a list before the loop,then append to it.
import os

img_lst = []
for img_scan in os.listdir('.'):
    if img_scan.endswith(('.jpg', '.png', '.jpeg', '.gif')):
        img_lst.append(img_scan)

print(img_lst)

Awesome!! Much appreciated, its the best feeling ever when I'm stuck, try 50+ things and nothing works then get a solution.

Feels good man Smile