Advertisement
Guest User

Python Bulk Rename Script

a guest
May 5th, 2014
5,946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. # os is a library that gives us the ability to make OS changes
  2. import os
  3.  
  4. def increment_file_numbers(directory, number):
  5.     # iterate over every file name in the directory
  6.     print directory
  7.     for file_name in os.listdir(directory):
  8.  
  9.         # we know that first two characters are numbers
  10.         print file_name
  11.         file_number = int(file_name[0:2])
  12.  
  13.         # we only increment the files that are bigger than number
  14.         if file_number > number:
  15.             new_file_number = file_number + 1
  16.  
  17.             # use string formatting to pad single-digit nums w/ a 0
  18.             new_file_name = '%s/%02d%s' % (directory, new_file_number, file_name[2:])
  19.             old_file_name = '%s/%s' % (directory, file_name)
  20.  
  21.             # rename the file!
  22.             os.rename(old_file_name, new_file_name)
  23.  
  24. # This is the path to these videos on my computer
  25. PATH = os.path.abspath('/Users/andrewbrown/Desktop/movie_test')
  26.  
  27. # Let's increment all the files w/ numbers above 11
  28. increment_file_numbers(PATH, 11)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement