我有很多像abc_model1.pdb.1,...,abc_model1.pdb.100。我想将这些文件更改为abc_model1_1.pdb,...,abc_model1_100.pdb。我尝试了几次不成功的尝试来修改“How do I rename the extension for a batch of files?”中给出的代码。我怎么能用python?
答案 0 :(得分:1)
试试这个:
import glob
import os
import shutil
file_dir = '/user/foo/bar/somewhere/'
dest_dir = file_dir # Change this to where you want
# the renamed files to go.
for file in glob.iglob(os.path.join(file_dir,'*.pdb.*')):
filename_temp, number = os.path.splitext(os.path.basename(file))
filename, ext = os.path.splitext(filename_temp)
shutil.move(file, os.path.join(dest_dir,
'{}_{}{}'.format(filename,number,ext)))