Python Forum
How to change existing date to current date in a filename?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change existing date to current date in a filename?
#1
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 .
Reply
#2
(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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 123 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 313 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Create dual folder on different path/drive based on the date agmoraojr 2 378 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Python date format changes to date & time 1418 4 518 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 961 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,161 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  PDF properties doesn't show created or modified date Pedroski55 4 1,007 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  How should I run pip-date in python3? newbieAuggie2019 5 1,775 Mar-31-2023, 03:21 PM
Last Post: snippsat
  How to see the date of installation of python modules. newbieAuggie2019 4 1,466 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,912 Dec-17-2022, 12:24 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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