Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.