Change a filename?
<p>I have some files in a folder named like this <code>test_1999.0000_seconds.vtk</code>. What I would like to do is to is to change the name of the file to <code>test_1999.0000.vtk</code>.</p>

<pre><code>import os

currentPath = os.getcwd() # get the current working directory
unWantedString = "_seconds"    

matchingFiles =[]
for path, subdirs, files in os.walk(currentPath):
    for f in files:
        if f.endswith(".vtk"): # To group the vtk files
            matchingFiles.append(path+"\\"+ f) # 

print matchingFiles

for mf in matchingFiles:
    if unWantedString in mf:
       oldName = mf
       newName = mf.replace(unWantedString, '')  # remove the substring from the string
       os.rename(oldName, newName) # rename the old files with new name without the string
</code></pre>

python