File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /usr/bin/python3.5
2+
3+ """
4+
5+ Author: Samrat Banerjee
6+ Dated: 26/06/2018
7+ Description: PROJECT- Renames filenames with American MM-DD-YYYY date format to European DD-MM-YYYY
8+
9+ """
10+
11+ import shutil , os , re
12+
13+
14+ # Create a regex that matches files with the American date format
15+ datePattern = re .compile (r"""^(.*?) # all text before the date
16+ ((0|1)?\d)- # one or two digits for the month
17+ ((0|1|2|3)?\d)- # one or two digits for the day
18+ ((19|20)\d\d) # four digits for the year
19+ (.*?)$ # all text after the date
20+ """ , re .VERBOSE )
21+
22+ # Loop over the files in the working directory
23+ for amerFilename in os .listdir ('.' ):
24+ mo = datePattern .search (amerFilename )
25+
26+ # Skip files without a date
27+ if mo == None :
28+ continue
29+
30+ # Get the different parts of the filename
31+ beforePart = mo .group (1 )
32+ monthPart = mo .group (2 )
33+ dayPart = mo .group (4 )
34+ yearPart = mo .group (6 )
35+ afterPart = mo .group (8 )
36+
37+ # Form the European-style filename
38+ euroFilename = beforePart + dayPart + '-' + monthPart + '-' + yearPart + afterPart
39+
40+ # Get the full, absolute file paths
41+ absWorkingDir = os .path .abspath ('.' )
42+ amerFilename = os .path .join (absWorkingDir , amerFilename )
43+ euroFilename = os .path .join (absWorkingDir , euroFilename )
44+
45+ # Rename the files
46+ print ('Renaming %s to %s....' % (amerFilename ,euroFilename ))
47+ shutil .move (amerFilename , euroFilename )
You can’t perform that action at this time.
0 commit comments