Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How To Modify This Code??
#1
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
Reply
#2
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)
Reply
#3
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.
Reply
#4
(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
Reply
#5
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"
Reply
#6
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)
Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Modify code from running program ? samuelbachorik 2 2,408 Jun-26-2020, 08:17 PM
Last Post: samuelbachorik
  Modify code BlackPimpernel 3 3,305 Apr-02-2018, 03:48 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020