1

I have a folder, C:\temp, with subfolders and files like below:

\11182014\

VA1122F.A14  
VA9999N.A14  
CT3452F.B13  
CT1467A.B14

\12012014\

MT4312F.B14  
MT4111N.B14  
CT4111F.A12

The file extensions are always an ".A" or ".B" followed by 2 digits. The file names always end with an "F", "A", or "N".

I would like to loop through all subfolders in C:\temp and:

  • prefix each file with "My_X_" where X is either an F, N, or A (i.e., the last letter in the file name)

  • suffix each file with "_" + the name of the subfolder

The result would be:

\11182014\

My_F_VA41245F_1182014.A14  
My_N_VA43599N_1182014.A14  
My_F_CT41111F_1182014.B13  
My_A_CT41112A_1182014.B14  

\12012014\

My_F_MT4312F_12012014.B14  
My_N_MT4111N_12012014.B14  
My_F_CT4111F_12012014.A12 

Any suggestions?

Larry
  • 183
  • 2
  • 12
  • 2
    have you tried search the documentation? I know that this sounds a little harsh, but a lot of what you're trying to do can be solved by perusing the documentation. – James Mertz Jul 15 '15 at 16:29
  • 1
    Or even doing a basic search in SO. http://stackoverflow.com/questions/2759067/rename-files-in-python – James Mertz Jul 15 '15 at 16:30

2 Answers2

1

This will do

fld = '/Your/path/to/main/folder/'

for root, subdirs, files in os.walk(fld):
    for name in files:
        curr_fld = os.path.basename(root)
        oldname = os.path.join(fld, curr_fld, name)
        splt_name =  name.split('.')
        myname = '_'.join(['My', splt_name[0][-1], splt_name[0], curr_fld + '.' + splt_name[1]])
        newname = os.path.join(fld, curr_fld, myname)
        os.rename(oldname, newname)
Katerina
  • 2,580
  • 1
  • 22
  • 25
  • If you get "Unicode Error" when running this... Add "r" before folder string to produce a raw string. fld = r'/Your/path/to/main/folder/' – G4mo Sep 18 '21 at 15:32
0

    #!/usr/bin/env python
# ---*--- coding:utf-8 ---*---

import os

path = "/home/username/test"

for root,dirname,filename in os.walk(path):
    for i in filename:
        i = i.split(".")
        first = i[1][0]
        last = i[0][-1]
        print "My_"+last+i[0]+root+"."+i[1]

  • Welcome to SO and thank you for posting an answer. Please consider adding context to your code. Also, please look through the [help center file on answering questions](http://stackoverflow.com/help/how-to-answer). – Richard Erickson Jul 15 '15 at 17:33
  • I'm new to Python so trying to get my head wrapped around the basic language. With korea60's code, the root value is returning the full path (e.g., "_C:/temp/test\11182014_"). I need it to return only the subfolder name (e.g., "_11182014_"). I tried replacing root with "dirname" and variations of os.path.basename(path), but can't get it. begin program. import os path = "C:/temp/test" for root,dirname,filename in os.walk(path): for i in filename: i = i.split(".") first = i[1][0] last = i[0][-1] print "My_"+last+"_"+i[0]+"_"+root+"."+i[1] end program. – Larry Jul 15 '15 at 18:07
  • The last row must change to this: print "My_"+last+i[0]+root.split("/")[-1]+"."+i[1] – korea60 Jul 15 '15 at 18:19
  • Thank you. I changed path from "C:/temp/test" to "C/temp/test/" and it worked. Now I'm trying to do the actual renaming. Reading a lot about os.rename but getting errors. I tried: os.rename(os.path.join(root, filename), "My_"+last+"_"+i[0]+"_"+root.split("/")[-1]+"."+i[1]) - This returns the folder where python is installed and an error about a string as left operand. Also tried: os.rename(root + os.sep + filename,"My_"+last+"_"+i[0]+"_"+root.split("/")[-1]+"."+i[1]) This returned "cannot concatenate 'str' and 'list' objects. – Larry Jul 15 '15 at 19:32
  • 1
    Can you add your comments on your answer please ? – Zulu Jul 16 '15 at 00:05