1

This question is a follow-up to this post: Python - rename files in subfolders based on subfolder and file name.

I am trying to loop through files within subfolders within C:\temp\, and rename each file in a specified way. Below is the code I have so far:

begin program.
import os
path = "C:/temp/"

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

When I run the line, print "My_"+last+"_"+i ..., it correctly shows that a file named VA1122F.A14 saved in C:\temp\11182014\ will be renamed My_F_VA1122F_11182014.A14.

However, the os.rename command returns this error:

"must be string, not list."

This seems be due to my use of "filename" in the os.rename command. From what I've read, the first argument for os.rename should be the old filename. When I run "print filename," it does indeed return a list of all files in the given subfolder, so that error makes sense. I just can't seem to figure out how to grab the old file name one at a time.

I also tried these but each returned an error:

os.rename(os.path.join(root, filename),"My_"+last+"_"+i[0]+"_"+root.split("/")[-1]+"."+i[1])

Error: Returns the folder where python is installed and an error about a string as left operand.

os.rename(root + os.sep + filename,"My_"+last+"_"+i[0]+"_"+root.split("/")[-1]+"."+i[1])

Error: cannot concatenate 'str' amd 'list' objects

I have scoured the documentation and many posts but cannot figure out what I'm missing. Thank you for any help.

Community
  • 1
  • 1
Larry
  • 183
  • 2
  • 12
  • As a hint you should do a `print` statement as such `print type(filename)`. I'd bet that doesn't come back as a `string`... – Matt Jul 22 '15 at 01:48

1 Answers1

2

You are trying to pass a list of filenames to rename command. You will get single filename in innerloop

for root,dirname,filenames in os.walk(path):   
     for filename in filenames:
        i = filename.split(".")

and you can use

os.rename(os.path.join(root, filename),"My_"+last+"_"+i[0]+"_"+root.split("/")[-1]+"."+i[1])

PS: Using '+' to concatenate strings is not highly recommended, so its better use string formatting.

os.rename(os.path.join(root, filename),"My_{last}_{start}_{sub_fold}.{ext}".format(last=last, start=i[0], sub_fold=root.split("/")[-1], ext=i[1]))
Jasim Muhammed
  • 1,376
  • 1
  • 20
  • 28
  • That gets me closer. `Print os.path.join(root, filename)` returns each file one at a time, e.g., `C:/temp/01142014\VA1122F.A14` (Should the last"\" be a "/"?). Also, when I used `os.walk('/tmp/')` nothing seemed to execute. So I used the original `os.walk(path).` However, now I get an "Access is denied error." I'm testing this on a computer where I have administrator privileges; however, I will be implementing this on my work computer where I am not permitted to have admin privileges. Is that essentially a show stopper? – Larry Jul 22 '15 at 16:06
  • I should add that I am able to rename files with os.rename without getting an Access is denied error. So maybe it is not an admin rights issue (or maybe the os.walk(path) makes it one. For example, I can rename a file with this code without any error: `begin program. import os rdir = 'c:/temp/01142014/' for num,fil in enumerate([fil for fil in os.listdir(rdir) if fil.endswith('.B14')]): os.rename(rdir + fil,rdir + 'My_' + fil) end program.` – Larry Jul 22 '15 at 16:21
  • Yea it had to be `path`. was testing on my machine. – Jasim Muhammed Jul 23 '15 at 05:40
  • Any idea what might be causing the `Access is denied` error? I seem to get it only when using os.walk. I don't get the error if I rename the files in a way that doesn't involve os.walk. – Larry Jul 23 '15 at 17:13