Python Forum
Date Format Changing Program Not Working - 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: Date Format Changing Program Not Working (/thread-1837.html)



Date Format Changing Program Not Working - pyth0nus3r - Jan-28-2017

So below is the program that I am using to change files with European (DD-MM-YYYY) style date to American (MM-DD-YYYY) Style Date:


Quote:#! python3
   # renameDates.py - Renames filenames with American MM-DD-YYYY date format
   # to European DD-MM-YYYY.

import shutil, os, re

# Create a regex that matches files with the American date format.
datePattern = re.compile(r"""^(.*?) # all text before the date
    ((0|1)?\d)-                     # one or two digits for the month
    ((0|1|2|3)?\d)-                 # one or two digits for the day
    ((19|20)\d\d)                   # four digits for the year
    (.*?)$                          # all text after the date
    """, re.VERBOSE)

# Loop over the files in the working directory.
for amerFilename in os.listdir('.'):
    mo = datePattern.search(amerFilename)

    # Skip files without a date.
    if mo == None:
        continue

    # Get the different parts of the filename.
    beforePart = mo.group(1)
    monthPart  = mo.group(2)
    dayPart    = mo.group(4)
    yearPart   = mo.group(6)
    afterPart  = mo.group(8)
    
    # Form the European-style filename.
    euroFilename = beforePart + dayPart + '-' + monthPart + '-' + yearPart + afterPart
    
    # Get the full, absolute file paths.
    absWorkingDir = os.path.abspath('.')
    amerFilename = os.path.join(absWorkingDir, amerFilename)
    euroFilename = os.path.join(absWorkingDir, euroFilename)
    
    # Rename the files.
    print('Renaming "%s" to "%s"...' % (amerFilename, euroFilename))
    shutil.move(amerFilename, euroFilename) 


However, the files whose days that have 20-29 in their dates does not work properly.
For example, ab12-10-2010test.txt works but ab22-10-2010test.txt does not work.
Is there some problem with the regular expression.


RE: Date Format Changing Program Not Working - metulburr - Jan-28-2017

Quote:So below is the program that I am using to change files with European (DD-MM-YYYY) style date to American (MM-DD-YYYY) 

you could just use datetime to convert the date. 

from datetime import datetime

time = '13-01-2017'

def convert(t):
    date_format = "%d-%m-%Y"
    convert_to_format = "%m-%d-%Y"
    return datetime.strptime(t, date_format).date().strftime(convert_to_format)

print(convert(time))
Output:
01-13-2017
Quote:For example, ab12-10-2010test.txt works but ab22-10-2010test.txt does not work.
then just extract the date portion of the string, convert it, then reinsert the old portion wrapped around the new date format and rename it. The input could be anything even 2-1-2017 and it will still output 01-02-2017


RE: Date Format Changing Program Not Working - Ofnuts - Jan-28-2017

(Jan-28-2017, 05:25 PM)pyth0nus3r Wrote: So below is the program that I am using to change files with European (DD-MM-YYYY) style date to American (MM-DD-YYYY) Style Date:

Quote:#! python3
   # renameDates.py - Renames filenames with American MM-DD-YYYY date format
   # to European DD-MM-YYYY.

Not very clear... changing European to American or vice-versa? Your regexp implies that your input is American (month first).

Several remarks:

  1. (0|1|2|3) is best replaced by [0123] or even [0-3]
  2. you can use re.sub (and back-references) to replace just the part that matches so you regexp doesn't have to deal with the bits outside the date part:
datePattern = re.compile(r'([01]?\d)-([0-3]?\d)-((19|20)\d\d)')

nameOut=re.sub(datePattern,r'\2-\1-\3',nameIn)
If nameOut==nameIn, the regexp was unmatched... or you where dealing with a file dated Jan 1st, Feb 2nd, etc... In any case you don't need to rename the file.