Skip to content

Instantly share code, notes, and snippets.

@samehkamaleldin
Last active June 29, 2018 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samehkamaleldin/74928f5f0f66022f38278300ce614559 to your computer and use it in GitHub Desktop.
Save samehkamaleldin/74928f5f0f66022f38278300ce614559 to your computer and use it in GitHub Desktop.
Record RENAMING
import os
from datetime import datetime, timedelta
from shutil import copyfile
# ------------------------------------------------------------------------------------
# [Things to Change] please input exact format of the date and time like the example
# ------------------------------------------------------------------------------------
records_directory = "/Users/sameh/Downloads/records"
site_name = "Cork_7"
first_date = "21/5/2012 14:31:51"
make_copies = True
# ------------------------------------------------------------------------------------
# OPTIONAL: Things named according to email information
# ------------------------------------------------------------------------------------
duration_in_minutes = 10
prefix = "SR"
extension = ".wav"
# ------------------------------------------------------------------------------------
dir_files = os.listdir(records_directory)
files_indices = [int(f[2: f.find(".")]) for f in dir_files if f.startswith(prefix) and f.endswith(extension)]
files_indices = sorted(files_indices)
datetime_format = '%d/%m/%Y %H:%M:%S'
current_datetime_obj = datetime.strptime(first_date, datetime_format)
time_10_minutes = timedelta(minutes=duration_in_minutes)
file_datetime_format = '%d_%m_%Y_%H_%M_%S'
new_records_directory = "/Users/sameh/Downloads/records_renamed"
if not os.path.isdir(new_records_directory):
os.mkdir(new_records_directory)
for idx in files_indices:
try:
file_path = os.path.join(records_directory, prefix + str(idx) + extension)
new_filename = current_datetime_obj.strftime(file_datetime_format)
if make_copies:
new_filepath = os.path.join(new_records_directory, site_name + "_" + new_filename + extension)
copyfile(file_path, new_filepath)
else:
new_filepath = os.path.join(records_directory, site_name+"_"+new_filename + extension)
os.rename(file_path, new_filepath)
current_datetime_obj += time_10_minutes
except:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment