0

I have a folder with long name which includes special character ("_") and date time of creation. What I want is to change it without the date and time information.

For example, I have a folder named:

This_a_folder_with_long_name_20_Oct_2015_07_10_20

I want to change it to:

This_a_folder_with_long_name by python script.

Any advice?

Maple Li
  • 43
  • 1
  • 9
  • 1
    I'm not the down-voter, but I will put in this comment: you should show what you've tried so far and what's not working with it (i.e., the code you have now, what you expect it to do, and what it actually does instead). See http://stackoverflow.com/help/mcve – torek Oct 26 '15 at 02:31
  • Is your question about how to process the name, or how to actually change folder names? – TigerhawkT3 Oct 26 '15 at 02:34
  • for your folder name '_20_Oct_2015_07_10_20' this part is same width? if it means '_day_month_year_hour_minute_seconds', I think it is same width, then you can remove same long characters for your folder name. – edwardramsey Oct 26 '15 at 02:42
  • @edwardramsey, you are correct, I am now searching a way to do so, thanks. – Maple Li Oct 26 '15 at 06:05

1 Answers1

1

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
Thane Plummer
  • 7,966
  • 3
  • 26
  • 30
  • Hi Thane Plummer, thanks a lot for the reply, one thing to highlight is the date and time value will change according to the real date and time. I try to change s = 'C:\test\This_a_folder_with_long_name_20_Oct_2015_07_10_20' to s = 'C:\testThis_a_folder_with_long_name*', but I get an error AttributeError: 'NoneType' object has no attribute 'start' – Maple Li Oct 26 '15 at 04:37
  • @MapleLi I did not include error checking in the output. The regular expression shown should match any date/time for the format you show. I'll include some example tests in an update so you can get a better idea of how it works. – Thane Plummer Oct 26 '15 at 04:44
  • Hi Thane Plummer, your script working properly, but since I cannot know the real date and time, I cannot get the exact folder name when running the script in production, the name I used in the question is a sample. – Maple Li Oct 26 '15 at 05:48