I'd like to remove only the middle part of the file name

Asked 2 years ago, Updated 2 years ago, 107 views

There are so many files such as "report_final_1.**" and report_final_2.**" I'm going to change it all to "Report 1.***."

I used os.path.split, but it doesn't work as I thought

python file-io

2022-09-21 15:27

1 Answers

Assuming that the extension *** of "report_final_1.**" is 3 characters,

import os
For filename in os.listdir("): #iterate all files in the current directory
    if filename.startswith ("Report_"):
        os.rename ("report" + filename[-5:])


2022-09-21 15:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.