Python Forum
Python: possibilty ro make a list of the file in the folder & change the name of file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python: possibilty ro make a list of the file in the folder & change the name of file
#1
I have a list of file. I am going to change the file name regarding to the modified date in below format. original file name:
Output:
2019-11-26_#001.tws 2019-11-26_#002.tws 2019-11-26_#004.tws ...
new name:
Output:
2019-11-26_#1.1.tws 2019-11-26_#1.2.tws 2019-11-26_#2.1.tws ...
the important point is; its possible the sequence of the filename would be different in some cases, i.e I did not have number 3 but the number 4 should be renamed to (count as) 2.1.

I can use the below code for renaming, but I dont know How to use loop to cover all name.
import os
import shutil
from os import path

def main():

        if path.exists("2019-11-26_#001.tws"):
                src = path.realpath("2019-11-26_#001.tws");
                os.rename('2019-11-26_#001.tws','2019-11-26_#1.1.tws')
if __name__ == "__main__":
    main()
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 314 Mar-03-2024, 12:16 AM
Last Post: MrBean12
Question How to add Python folder in Windows Registry ? Touktouk 1 243 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 391 Feb-15-2024, 11:15 AM
Last Post: rawatg
  connect sql by python using txt. file dawid294 2 426 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 525 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  file open "file not found error" shanoger 8 1,087 Dec-14-2023, 08:03 AM
Last Post: shanoger
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,426 Nov-09-2023, 10:56 AM
Last Post: mg24
  Search Excel File with a list of values huzzug 4 1,216 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Replace a text/word in docx file using Python Devan 4 3,290 Oct-17-2023, 06:03 PM
Last Post: Devan
  Help creating shell scrip for python file marciokoko 10 1,346 Sep-16-2023, 09:46 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020