Skip to content

Instantly share code, notes, and snippets.

@bhavul
Created June 18, 2017 12:45
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 bhavul/a850bdc045d3745afe873a3f40c6be86 to your computer and use it in GitHub Desktop.
Save bhavul/a850bdc045d3745afe873a3f40c6be86 to your computer and use it in GitHub Desktop.
import os
import sys
from datetime import datetime
def main():
for afile in os.listdir(os.getcwd()):
fileBreak = afile.split('.')
extension = fileBreak[1]
if extension == "3gp":
datePhoneBreak = fileBreak[0].split('p')
phoneNum = datePhoneBreak[1]
dateFin = datePhoneBreak[0]
dateFin = dateFin[2:16]
# format phone number (remove extension)
if phoneNum[0] == '+':
phoneNum = phoneNum[3:]
elif phoneNum[0] == '0':
phoneNum = phoneNum[1:]
#format date - strptime, strftime
dateobj = datetime.strptime(dateFin,'%Y%m%d%H%M%S')
dateInName = dateobj.strftime('%d_%b_%Y_%I_%M_%p')
#pick contact name
contactName = ""
datafile = open("phonenumbers.txt","r")
for line in datafile.readlines():
if(line.find(phoneNum) >= 0):
lineBreak = line.split(':')
contactName = lineBreak[1]
datafile.close()
contactName = contactName.strip()
final_name = contactName+'_'+dateInName+'.'+extension
# FINAl STEP - RENAMING...
os.rename(afile,final_name)
else:
print "other extension."
# do nothing. Some other extension
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment