Advertisement
ceetan

Copy and rename files

Jan 27th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import os
  2. import re
  3. import shutil
  4.  
  5. input_directory = '/Users/Kristina/PycharmProjects/testing/input'
  6. output_directory = '/Users/Kristina/PycharmProjects/testing/output'
  7.  
  8. for filename in os.listdir(input_directory):
  9.     match_result = re.match('(.+)(\d{2})-(\d{2})-(\d{4})(.+)', filename)
  10.     if (match_result):
  11.         filename_first_part = match_result.group(1)
  12.         month = match_result.group(2)
  13.         day_of_month = match_result.group(3)
  14.         year = match_result.group(4)
  15.         filename_last_part = match_result.group(5)
  16.         new_filename = filename_first_part + year + '.' + month + '-' + day_of_month + filename_last_part
  17.         full_source_path = os.path.join(input_directory, filename)
  18.         full_destination_path = os.path.join(output_directory, new_filename)
  19.         print('Copying file ' + full_source_path + ' to ' + full_destination_path)
  20.         shutil.copy2(full_source_path, full_destination_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement