Python Forum

Full Version: How to change existing date to current date in a filename?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 .
(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'