Skip to content

Instantly share code, notes, and snippets.

@wrybread
Last active May 1, 2023 16:34
Show Gist options
  • Save wrybread/5062ed4a4fb56faea5a437a1170e9f31 to your computer and use it in GitHub Desktop.
Save wrybread/5062ed4a4fb56faea5a437a1170e9f31 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
'''
This solves a problem in DJI drone cameras where the file numbers never go above DJI_0999.JPG.
This will add 1,000,000 to the file number. For example it'll change DJI_0999.JPG to DJI_1000999.JPG
And it's smart enough to know that the file has already been processed (won't add 1,000,000 to the file
number if the number is already above 1,000,000).
It's also smart enough to handle files with extra text and numbers added, like "DJI_0088 - Part 2.MP4"
[email protected] 4/30/2023
'''
from __future__ import print_function
import sys, os, re
# how much to increment the file number by
increment_amount = 1000000
# Where are the files to process? (Default is directory of script)
directory = os.path.dirname( os.path.abspath(sys.argv[0]) )
#-------------------------------------------
listing = os.listdir(directory)
for f in listing:
full_filename_path = os.path.join(directory, f)
if f[:4] == "DJI_":
# gets the number in the filename. For example "DJI_0991.MP4" and "DJI_0991 - part 2.MP4" both return "0991"
file_number = re.findall('DJI_(.+?)\W', f) [0]
file_number_int = int(file_number)
if file_number_int < 1000:
# increment it
new_file_number = file_number_int + increment_amount
# re-compose the filename (there's probably a more graceful way to do this)
new_filename = "DJI_" + str(new_file_number) + f.replace("DJI_"+file_number, "")
new_filename_path = os.path.join(directory, new_filename)
print ("Renaming %s to %s" % (f, new_filename) )
# rename it! (comment out for testing obviously)
os.rename(full_filename_path, new_filename_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment