重命名/重新构建目录中文件名的布局

问题描述 投票:1回答:1
import os

input_path = raw_input('Input file path here : ')

os.chdir(input_path)


for f in os.listdir(input_path):
    print f

我有“UDIM”纹理文件,这些文件被枚举

1001_Base_Color.png, 1002_Base_Color.png, 1003_Base_Color.png.

我的目标是遍历目录中的每个文件,并重新定位文件末尾的数字。 name ---> Base_Color_1001.png

任何见解都表示赞赏!

谢谢

python file directory batch-rename renaming
1个回答
2
投票

您可以通过下划线拆分每个文件名,然后在将数字附加到标记末尾后使用下划线重新加入它们:

for f in os.listdir(input_path):
    name, ext = os.path.splitext(f)
    os.rename(f, ''.join(name.partition('_')[::-1]) + ext)
© www.soinside.com 2019 - 2024. All rights reserved.