This is just a basic outline of what you need to do, not a complete solution. Be sure to add error checking on the folder rename, and you also need to decide what to do if you try to shorten a name that doesn't match the regular expression. Use this online tester to understand the regular expression.
from __future__ import print_function
import os
import re
def shortname(s):
# An ugly regular expression that finds your date time string.
m = re.search(r'_\d{1,2}_[a-zA-Z]{3}_\d{4}_\d{1,2}_\d{1,2}_\d{1,2}\Z', s)
# Get your file name without the date time string
if m is not None:
return s[:m.start()]
print("No match found - return original string")
return s
s = r'C:\test\This_a_folder_with_long_name_20_Oct_2015_07_10_20'
s2 = r'C:\test\This_another_folder_with_long_name_11_Oct_2014_2_1_25'
# Test the output
newname = shortname(s)
print("Long name:", s)
print("New name:", newname)
newname = shortname(s2)
print("Long name:", s2)
print("New name:", newname)
# Only rename if the name is different
if newname != s:
# You should do error checking before renaming.
# Does the directory already exist?
os.rename(s, newname)
Output:
Long name: C:\test\This_a_folder_with_long_name_20_Oct_2015_07_10_20
New name: C:\test\This_a_folder_with_long_name
Long name: C:\test\This_another_folder_with_long_name_11_Oct_2014_2_1_25
New name: C:\test\This_another_folder_with_long_name