Python Forum

Full Version: Python: possibilty ro make a list of the file in the folder & change the name of file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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