2016-09-09 9 views
1

Ich habe einige Dateien in einem Ordner mit dem Namen test_1999.0000_seconds.vtk. Ich möchte den Namen der Datei in test_1999.0000.vtk ändern.Ändern Sie einen Dateinamen?

+3

Was haben Sie versucht? Siehe http://stackoverflow.com/questions/2759067/rename-files-in-python – Maxim

Antwort

3

können Sie verwenden os.rename

os.rename("test_1999.0000_seconds.vtk", "test_1999.0000.vtk") 
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 
Verwandte Themen