Python Forum

Full Version: make a list of the file in the folder and change the name of file regarding to time
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:
2019-11-26_#001.tws
2019-11-26_#002.tws
2019-11-26_#004.tws ...

new name:

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
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()
You could use a dictionary
corresp = {'#001.tws': '#1.1.tws', '#002.tws': '#1.2.tws', '#004.tws': '#2.1.tws'}
name = 'spam#002.tws'
if name[-7:] in corresp:
    new_name = name[:-7] + corresp[name[-7:]]
thanks for reply, something that i have not understand is, should i use dic in the fucntion line?

general can you explain the code ? thanks
I am not sure I understand the question. You can use a single dict that maps the endings of the file names to their new endings. The dict allows you to compute the new names of the files from the old ones. It remains to move the files.
LOOKUP_TABLE = {
    '001.tws': '1.1.tws',
    '002.tws': '1.2.tws',
    '004.tws': '2.1.tws',
}

# LOOKUP_TABLE["004.tws"] -> "2.1.tws"
# LOOKUP_TABLE.get("004.tws") -> "2.1.tws"
# LOOKUP_TABLE.get("not in dict", 42) -> 42
# https://docs.python.org/3/tutorial/datastructures.html#dictionaries


def transform_name(name):
    """
    Convert the ending of the name from LOOKUP_TABLE
    If the key does not exist in the LOOKUP_TABLE
    return the name unmodified.
    """
    filename, sep, ending = name.partition('#')
    # https://docs.python.org/3/library/stdtypes.html#str.partition
    ending = LOOKUP_TABLE.get(ending, ending)
    return filename + sep + ending


print(transform_name("2019-11-26_#001.tws"))
Just follow the links. The dict is of the most important data structures in Python.
A dict is a mapping with keys, which point to values. In a dict, you've only unique keys.
The original file-extension is saved as key and the key points to the new extension.
The difference here is, that I used str.partition to split the string into filename, seperator (#) and the extension.

Maybe this Solution is not what you want.
Is there a rule how the extension should converted?
Is there a calculation for it?

Otherwise you have to make for each possible extension an entry in the LOOKUP_TABLE.

A possible way to calculate this values, but not the same as your example:
def calculate_extension(text):
    number, sep, extension = text.partition('.')  # partition at .
    before_sep, after_sep = divmod(int(number), 2)
    return f'{before_sep}{sep}{after_sep}{sep}{extension}'
A calculated lookup_table just to show:
{'000.tws': '0.0.tws',
 '001.tws': '0.1.tws',
 '002.tws': '1.0.tws',
 '003.tws': '1.1.tws',
 '004.tws': '2.0.tws',
 '005.tws': '2.1.tws',
 '006.tws': '3.0.tws',
 '007.tws': '3.1.tws',
 '008.tws': '4.0.tws',
 '009.tws': '4.1.tws',
 '010.tws': '5.0.tws',
 '011.tws': '5.1.tws',
 '012.tws': '6.0.tws',
 '013.tws': '6.1.tws',
 '014.tws': '7.0.tws',
 '015.tws': '7.1.tws',
 '016.tws': '8.0.tws',
 '017.tws': '8.1.tws',
 '018.tws': '9.0.tws',
 '019.tws': '9.1.tws'}
Usually this is what a programmer does.
Instead of filling up a lookup-table,
we could calculate the expected values programmatically.
@DeaD_EyE
Thanks for information and code.

Problem is, that I have not same name everyday. the name will change by date.
eg. today would be :
2020-02-21#001.tws

and also it would be possible that the sequence of numbering did not happened in the naming process.