Renaming OS files
<p>I am trying to rename files based on their extensions. Below is my code, Somehow my os.rename isn't working. There aren't any errors though. I got no idea what is wrong. Hope you guys could help. Thanks.</p>

<pre><code>import os
import glob
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)
file_number = len(matching_files)



for filename in os.listdir(directory):
    if ext in filename:
       path = os.path.join(directory, filename)
       seperated_names = os.path.splitext(filename)[0]
       replace_name = filename.replace(seperated_names, r)
       split_new_names = os.path.splitext(replace_name)[0]

for pad_number in range(0, file_number):
    padded_numbers = "%04d" % pad_number
    padded_names = "%s_%s" % (split_new_names, padded_numbers)
    newpath = os.path.join(directory, padded_names)
    newpathext = "%s%s" % (newpath, ext)


new_name = os.rename(path, newpathext) 
</code></pre>
