Question

Given that a list of file names are:

shortname = [H04_IF_FigF2_LCC_05Apr12_mm, H04_BS_IF_FigF2_LCC_05Apr12_mm_2, HH_IF_FigF2_SS_05Apr12_mm, D01_BS_IF_FigF2_LCC_05Apr12_mm_1, D01_BS_FigF2_Overview_05Apr12_mm]

I want to disect each file name by each underscore:

for x,y in enumerate(shortname.split("_")):

I want to update the date to the current date:

strftime("%d%b%y")

and then glue it all back together using underscores, but I want it to be able to interactively do this as the count of "parts" of the file name are different and so is the position that the date "part" lies (i.e. in the first name, there are 6 "parts" and the date is the 5th "part", in the second, there are eight and the date is the 6th "part")

I'm trying to use the enumerator to get a list of numbers and text strings. I played around with getting a range of x and iterating through each file name part, but I'm stuck on trying to isolate the date from the rest and then how to interactively glue the parts back together.

Any suggestions? Thanks, Mike

Was it helpful?

Solution

I'd simply try to parse everything as the date, and then update if it succeeds:

import datetime

shortnames = ['H04_IF_FigF2_LCC_05Apr12_mm', 
             'H04_BS_IF_FigF2_LCC_05Apr12_mm_2', 'HH_IF_FigF2_SS_05Apr12_mm', 
             'D01_BS_IF_FigF2_LCC_05Apr12_mm_1', 'D01_BS_FigF2_Overview_05Apr12_mm']

def update_time(name):
    old_split = name.split('_')
    new_split = []
    date_fmt = '%d%b%y'
    new_date = datetime.datetime.strftime(datetime.datetime.now(), date_fmt)
    for n in old_split:
        try:
            old_date = datetime.datetime.strptime(n, date_fmt)
            new_split.append(new_date)
        except ValueError:
            new_split.append(n)
    return '_'.join(new_split)

>>> [update_time(filename) for filename in shortnames]
['H04_IF_FigF2_LCC_10Apr12_mm', 'H04_BS_IF_FigF2_LCC_10Apr12_mm_2', 'HH_IF_FigF2_SS_10Apr12_mm', 'D01_BS_IF_FigF2_LCC_10Apr12_mm_1', 'D01_BS_FigF2_Overview_10Apr12_mm']

This way I don't have to care about any of the internal details.

OTHER TIPS

Will this work for you?

>>> from datetime import datetime
>>> name = ["H04_IF_FigF2_LCC_05Apr12_mm", "H04_BS_IF_FigF2_LCC_05Apr12_mm_2", "HH_IF_FigF2_SS_05Apr12_mm", "D01_BS_IF_FigF2_LCC_05Apr12_mm_1", "D01_BS_FigF2_Overview_05Apr12_mm"]
>>> now = datetime.strftime(datetime.now(),"%d%b%y")
>>> ["_".join(i.split("_")[:-2]+[now]+[i.split("_")[-1]]) for i in name]
['H04_IF_FigF2_LCC_10Apr12_mm', 'H04_BS_IF_FigF2_LCC_05Apr12_10Apr12_2', 'HH_IF_FigF2_SS_10Apr12_mm', 'D01_BS_IF_FigF2_LCC_05Apr12_10Apr12_1', 'D01_BS_FigF2_Overview_10Apr12_mm']

You can keep the result of the splitting, replace the selected part and then join again to form the new filename:

for fn in shortname:
    parts = fn.split("_")
    # Ask the user about what date to replace
    #(skipped, result in variable i)
    parts[i] = datetime.date.today().strftime("%d%b%y")
    newname = "_".join(parts)
    os.rename(fn, newname)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top