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
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,285 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to remove unwanted images and tables from a Word file using Python? rownong 2 824 Feb-04-2025, 08:30 AM
Last Post: Pedroski55
  Best way to feed python script of a file absolut 6 1,185 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  Removal of watermark logo pdf file Python druva 0 773 Jan-01-2025, 11:55 AM
Last Post: druva
  How to write variable in a python file then import it in another python file? tatahuft 4 994 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  How to communicate between scripts in python via shared file? daiboonchu 4 1,843 Dec-31-2024, 01:56 PM
Last Post: Pedroski55
  Problems writing a large text file in python Vilius 4 1,071 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,204 Nov-24-2024, 12:58 PM
Last Post: deanhystad
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,096 Nov-21-2024, 11:48 PM
Last Post: haihal
  How to re-register .py file extension to new moved Python dir (on Windows)? pstein 5 1,409 Nov-06-2024, 03:06 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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