0

I am trying to find and rename below files using below script

IYBR2C.YGPSWS.SL.1.M.20190503170641.csv to SAK_BB_AL.csv
QWEJ1P.YGPSWS.SL.1.M.20190508122932.csv to SAM_SG_MO.csv
SCPR1C.YGPSWS.SL.1.T.20190503164120.csv to SVM_SG_AL.csv
SMCE2P.SGPAWS.SL.1.T.20190508123138.csv to SDM_SG_MO.csv

This is what I have tried for a single file:

But my problem is I need to have some conditions on this renaming that is

ex-:
IYBR2C.YGPSWS.SL.1.M.20190503170641.csv to SAK_BB_AL.csv

for this example file I need to check whether it starts with "IYBR2C" and it contains "M" if this two conditions are met then rename it to SAK_BB_AL.csv

below is what I have tried but no results for starts with ("IYBR2C") and can I check that it contains the letter ("M") as well.

for root, dirs, files in os.walk(/tmp):
for dir in dirs:
    if dir.startswith("IYBR2C"):
        org_fp = os.path.join(root, dir)
        new_fp = os.path.join(root, dir[1])
        os.rename(org_fp, new_fp)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.