From: Alister Ware on
On Fri, 13 Aug 2010 07:40:42 -0700, blur959 wrote:

> Hi all, I got a problem with my script. Everything looks good so far but
> for some reason my os.rename isn't working. Can anyone tell me why? Hope
> you guys could help. Thanks.
>
<snip>

You have a number of logic flaws in your code.
1st you do not actually need to know how many matching files you have.
2nd you need to rename the file inside the same loop that is splitting
your file name (this is why you are failing, your rename loop has the
same filename each time around),

I knocked up a quick hack loosely based on your original code that will
generate the file names for you. it has some stratigic print statements
so you can see what is happening (always a good idea when debugging code)

I have left the actual re-naming of the files for you to complete,
I would also agree with the earlier suggestion that you work through some
on line tutorials

import os
import glob
def rename(filelist): #function to add a count to each filenale in a list
count=0
for fullname in filelist:
count+=1
print "full file path %s" % fullname
path,filename=os.path.split(fullname)
name,ext=os.path.splitext(filename)
print "path: '%s' Name: '%s' Extn: '%s'" % (path,name,ext)
newname="%s_%04d.%s" %(name,count,ext)
print "New filename: '%s'" % newname
# rename filename to newname goes here
# dont forget you also need path

directory = raw_input("directory? ")
ext = raw_input("file extension? ")
r = raw_input("replace name")
pattern = os.path.join(directory, "*." + ext)
matching_files = glob.glob(pattern)
rename(matching_files)


--
Nothing makes one so vain as being told that one is a sinner.
Conscience makes egotists of us all.
-- Oscar Wilde