I am wondering if this is a safe and/or sensible way of going about doing the following task:

My problem is that I have a folder with loads of directories with names of form YYYY-MM-DD. They are from my camera. Each directory contains picture files. The problem is that the picture files are designated by just 4 digits like C4018.jpeg. Hence after a few days of shooting I will find myself with duplicate file names! My objective is then to just add the date of the containing folder to each file, which solves my problem.

Here is my current solution. I tested it so that it also doesn't keep adding the date each time I run the code - once the files have been renamed they are not renamed in the future.

Am I safe running this code? By safe, I mean I don't end up with a renaming catastrophe. Also, please feel free to comment on the efficiency and style of my code.

 import os
 import re
 rootdir = '/Volumes/Misc/Pictures/test/' #Location of video folders to rename

  # Regexp for YYYY-MM-DD or YYYY/MM/DD
  date_re = re.compile('\d{4}[-/]\d{2}[-/]\d{2}')
  subdirs = [subdir[0] for subdir in os.walk(rootdir)]

  for subdir in subdirs:
      match = date_re.findall(subdir)
      if match:
          for (_,_,files) in os.walk(os.getcwd()):
              for f in files:
                  match2 = date_re.findall(f)
                  if not match2:
                      new_name = match[0] + '-' + f
                      os.rename(f, new_name)
  • \$\begingroup\$ I would suggest using os.walk as a for loop, that's the generally accepted use for it. such as for root, dirs, files in os.walk('python/Lib/email'). \$\endgroup\$
    – R Nar
    Dec 10, 2015 at 23:07
  • \$\begingroup\$ Thanks - other than that is there anything immediately problematic with this code? \$\endgroup\$
    – Jack
    Dec 10, 2015 at 23:12

1 Answer 1


Why don't you use the exif timestamps of your images and name them accordingly? I use the following code to rename my images. It also extends the timestamp to feature not only the date but also the time:

import os
import glob
from PIL import Image
from PIL.ExifTags import TAGS
import time  

def get_exif(fn):
    ret = {}
    i = Image.open(fn)
    info = i._getexif()
    for tag, value in info.items():
        decoded = TAGS.get(tag, tag)
        ret[decoded] = value
    return ret


files = glob.glob("*.JPG")

for file in files:
    time = get_exif(file)["DateTimeOriginal"]

    time = time.replace(":", "")
    time = time.replace(" ", "_")
    number = 0
    new_name = time+"_additional_information.jpg"
    if new_name == file:
        print(new_name, "already ok")
    while os.path.exists(new_name):
        number += 1
        new_name = time+"_"+str(number)+"_additional_information.jpg"
    os.rename(file, new_name)

If you want to use your own code, why don't you just test it on a copy of the data?

  • \$\begingroup\$ Thanks - I did test it and it works fine, I think what I wanted was more of a review on my code as was mentioned in one of the comments above. \$\endgroup\$
    – Jack
    Dec 10, 2015 at 23:38
  • \$\begingroup\$ Yes, I already thought so, but nevertheless wanted to share my code, as I thought it might be useful for your project. Your code looks ok, but I would test it on a copy of the dataset before. \$\endgroup\$
    – Randrian
    Dec 11, 2015 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.