如何使用Python删除多个文件名中的相同部分?

问题描述 投票:0回答:0

我正在尝试使用 Python 删除多个文件名中的相同部分,但循环没有得到迭代。它只是输入路径,但不会从文件名中删除相同的部分。

import os

for root_dir,cur_dir,file_name in os.walk("D:\Python\Scripts"):
    if "new_" in file_name:
    old_name=file_name
    new_name=file_name.replace("new_","")
    os.rename(old_name,new_name)

文件夹里面有子文件夹需要替换文件名所以我在os.walk函数中使用了root_dir,cur_dir

Input file Names in directory:
New_File1.xlsx
New_Data1.xlsx
New_Task1.xlsx

Output file names should be:
File1.xlsx
Data1.xlsx
Task1.xlsx
python-3.x
© www.soinside.com 2019 - 2024. All rights reserved.