Python Forum
How to change existing date to current date in a filename? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to change existing date to current date in a filename? (/thread-17598.html)



How to change existing date to current date in a filename? - shankar455 - Apr-17-2019

Suppose i have a list of files in a directory as mentioned below
1. Shankar_04152019_ny.txt
2. Gopi_shan_03122019_mi.txt
3. Siva_mourya_02242019_nd.txt
..
.
.
.
.
1000 . Jiva_surya_02282019_nd.txt

query : At one shot i want to modify the above all filenames present in one path with current date as mentioned like below
Since todays date is :04172019
1. Shankar_04172019_ny.txt
2. Gopi_shan_04172019_mi.txt
3. Siva_mourya_04172019_nd.txt.....etc

Kindly suggest me script .


RE: How to change existing date to current date in a filename? - snippsat - Apr-17-2019

(Apr-17-2019, 12:00 PM)shankar455 Wrote: Kindly suggest me script .
I don't work like this here,you have to show some effort
Can give some hint that should take you close if put together.
import os

path = r'some_path'
for file in os.scandir(path):
    if file.name.endswith('.txt'):
        # Do something to all .txt
>>> import re 
>>> 
>>> today = '04172019'
>>> s = 'Siva_mourya_02242019_nd.txt'
>>> re.sub(r'\d+', today, s)
'Siva_mourya_04172019_nd.txt'