2

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

Community
  • 1
  • 1
Haris
  • 13,645
  • 12
  • 90
  • 121
  • 3
    possible duplicate of [Python glob multiple filetypes](http://stackoverflow.com/questions/4568580/python-glob-multiple-filetypes) – Dakkaron Aug 12 '15 at 08:33
  • I already mentioned in my question that I found that link, only confusion is how to apply here in my case. – Haris Aug 12 '15 at 08:37
  • Hardly more than copy-paste. See my answer, that is really just copy-paste. – Dakkaron Aug 12 '15 at 08:58

2 Answers2

8

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.

Dakkaron
  • 5,930
  • 2
  • 36
  • 51
1

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))
LexyStardust
  • 1,018
  • 5
  • 18