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
.
Asked
Active
Viewed 1,105 times
1
-
3What have you tried? See http://stackoverflow.com/questions/2759067/rename-files-in-python – Maxim Sep 09 '16 at 09:15
2 Answers
3
You can use os.rename
os.rename("test_1999.0000_seconds.vtk", "test_1999.0000.vtk")

Francesco Nazzaro
- 2,688
- 11
- 20
0
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

Thangasivam Gandhi
- 349
- 2
- 6