Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python glob multiple extensions

Tags:

python

Hi I need to create file which contain image path in following format

/var/sample/55/cam.png;55
/var/sample/55/desktop.png;55
/var/sample/44/92x92.png;44
/var/sample/44/full.png;44

I am using the following python script for doing this,

BASE_PATH=sys.argv[1]
SEPARATOR=";"
f = open('path.csv', 'w')

for dirname, dirnames, filenames in os.walk(BASE_PATH):
    for subdirname in dirnames:
        subject_path = os.path.join(dirname, subdirname)
    list = glob.glob(subject_path+'/*.png')
        for filename in list:
            abs_path = "%s" % (filename)
            print "%s%s%s" % (abs_path, SEPARATOR, subdirname)
    print >>f, "%s%s%s" % (abs_path, SEPARATOR, subdirname)
f.close()

But the above code will list only .png extension now, I need to include .jpg,.jpeg etc.. How can I edit the above code to achieve it.

I have already found the answer here but not able to understand how to apply here.

Thanks,
Haris

like image 495
Haris Avatar asked Jan 28 '26 22:01

Haris


2 Answers

What you need to do is glob for each extension. glob.glob() returns you a list, which you can then append to the rest.

So for example you could do this:

extensions = ("*.png","*.jpg","*.jpeg",)
list = []
for extension in extensions:
    list.extend(glob.glob(subject_path+"/"+extension)

or more concise using a list comprehension:

list = sum([ glob.glob(subject_path+"/"+x) for x in ("*.png","*.jpg","*.jpeg") ], [])

So glob.glob() returns a list of all files that were globbed with that extension. With list.extend() you can append one list to another, so you just add all these glob results together into one list.

like image 114
Dakkaron Avatar answered Feb 02 '26 11:02

Dakkaron


Glob uses the fnmatch module at the back end, maybe you can just use that directly yourself? Here's an approach with the checking out in a function to keep the body of your code cleaner:

eg.

import sys
import fnmatch
import os


def filter_files(file_list, ext_list):
    for file in file_list:
        for ext in ext_list:
            if fnmatch.fnmatch(file, ext):
                yield file
                break

BASE_PATH=sys.argv[1]

EXTENSIONS= "*.jpg", "*.png", "*.gif"

for dirname, dirnames, filenames in os.walk(BASE_PATH):
    matches = list(filter_files(filenames, EXTENSIONS))
like image 21
LexyStardust Avatar answered Feb 02 '26 13:02

LexyStardust