更改文件名?

时间:2016-09-09 09:14:00

标签: python

我在名为test_1999.0000_seconds.vtk的文件夹中有一些文件。我想要做的是将文件名更改为test_1999.0000.vtk

2 个答案:

答案 0 :(得分:3)

您可以使用os.rename

jQuery("td.jz-pl select").change(function() {       
    jQuery(this).closest('tr').find('td.jz-en select').css('background-color','red');
});

答案 1 :(得分: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