top of page

Listing File Extensions

This challenge was to create a code to look into the desired folder and pull out only the file extensions of either .exr or .jpg images.

The process to do so is as follows.

1. Locate the folder where the images may be.

2. Use regular expressions to create a capture group for the code to extract later.

3. Make an empty list for the extensions.

4. Get a listing of all .exr or .jpg extensions

5. For every file, check to see if the criteria matches, and if so, add the numeric extension to the list.

The code for this operation is shown below -

import os
import os.path
import re
import inspect
import glob

fullpath = inspect.getframeinfo(inspect.currentframe()).filename
dirpath = os.path.dirname(os.path.abspath(fullpath))

glob_pattern = os.path.join(dirpath, '*.exr')
paths = glob.glob(glob_pattern)

re_pat = re.compile('(\w+)[._](\d+)[._](exr|jpg)')
extensions = []

for s in paths:
    found_it = re.search(re_pat, s)
    if found_it:
        extensions.append('%s' % found_it.group(2))
print ('Found the following file extensions', extensions)


I also created another code so that whichever folder the code was in, it would run the operation for that folder using the same code as the file renaming process.

python extend.py

read

For windows, it would be a .bat file.

C:/python27/python extend.py

pause

bottom of page