Reputation: 41
I have some files in a folder named like this test_1999.0000_seconds.vtk
. What I would like to do is to is to change the name of the file to test_1999.0000.vtk
.
Upvotes: 1
Views: 1113
Reputation: 379
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
Upvotes: 0
Reputation: 2916
You can use os.rename
os.rename("test_1999.0000_seconds.vtk", "test_1999.0000.vtk")
Upvotes: 3