Listing Numeric Extensions Script

This page explains the development of a python script that can list the numeric file extensions of a sequence of image files.

 
 

ALGORITHM

1. Find the path to the parent ("images") directory.
2. Make a "regular expression pattern" ("re")
3. Make an empty list - to contain the numeric extensions.
4. Get a listing of all the .exr files.
5. For each file in the listing:
     5.1 check its name matches the "re",
     5.2 if a match is found use the "re" to grab the numeric extension,
     5.3 add the numeric extension to the list.
4. Print a confirmation of the action(s) taken.

 

RUNNING THE CODE

1. Copy the two scripts to the folder containing the images to be renamed.
2. On Linux double click the file called "numeric_extensions", on Windows double click the file called "numeric_extensions.bat".

 

THE CODE - "numeric_extensions.py"

import re
import os
import os.path
import glob
import inspect
  
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 path in paths:
    fname = os.path.basename(path) 
    found_it = re.search(re_pat, fname)
    if found_it:
        extensions.append('%s' % found_it.group(2))
        extensions.sort()
print extensions
 

SHELL SCRIPTS

Linux - "numeric_extensions"

python numeric_extensions.py
read

Windows - "numeric_extensions.bat"

C:\python27\python numeric_extensions.py
pause

 

RESTRICTIONS

This is meant as an example for the process of listing numeric extensions for a sequence of image files. The code assumes that the files are in an image file format(specifically .exr and .jpg) and are located in a specific directory that may differ from user to user. This code should be use as a starting point and should be tailored to suit the user's needs.