-
Notifications
You must be signed in to change notification settings - Fork 9
/
renameDates.py
47 lines (41 loc) · 1.04 KB
/
renameDates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!python3
#renameDates.py - Rename filenames with American MM-DD-YYYY date format to European DD-MM-YYYY
import os,shutil,re
#为美国风格的日期建立一个正则表达式
date = re.compile(r'''
^(.*?)
((0|1)?\d)-
((0|1|2|3)?\d)-
((19|20)\d\d)
(.*?)$
''',re.VERBOSE)
#Loop over the file in the working directory.
for filename in os.listdir('.'):
mo = date.search(filename)
#Skip files without date.
if mo == None:
continue
#Get the different parts od the filename.
beforPart = mo.group(1)
monthPart = mo.group(2)
dayPart = mo.group(4)
yearPart = mo.group(6)
afterPart = mo.group(8)
"""
date = re.compile(r'''
^(1)
(2(3))-
(4(5))-
(6(7))
(8)$
''',re.VERBOSE)
"""
#Form the European-style filename.
euroDate = beforPart + dayPart +'-'+ monthPart +'-'+ yearPart + afterPart
#Get the full,absolute file paths
absPath = os.path.abspath('.')
amerPath = os.path.join(absPath,filename)
EuroPath = os.path.join(absPath,euroDate)
#Rename the file.
print('Rename %s to %s'%(amerPath,EuroPath))
shutil.move(amerPath,EuroPath)