-
Notifications
You must be signed in to change notification settings - Fork 148
/
project_7.py
52 lines (33 loc) · 1.31 KB
/
project_7.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
45
46
47
48
49
50
51
# Renaming Files with American-Style date format to European-Style date format
# (MM-DD-YYYY) --> American-Style format
# (DD-MM-YYYY) --> European Style format
# Searching the filename in the current working directory for American-Style dates.
import shutil
import os
import re
american_regex = re.compile(r'''^(.*?) # Text before date
((0|1)?\d)- # month
((0|1|2|3)?\d)- # day
((19|20)\d\d) # four digit year
(.*?)$
''',re.VERBOSE)
# Looping over the current working directory for American Filenames
for american_file in os.listdir('.'):
match1 = american_regex.search(american_file)
# If file not found then don't break out of the loop
if match1 == None:
continue
# Grouping the date format
before_date = match1.group(1)
month_part = match1.group(2)
day_part = match1.group(4)
year_part = match1.group(6)
after_part = match1.group(8)
# European-Style format
european_file = before_date + day_part + '-' + month_part + '-' + year_part + after_part
# Get the absolute path of both files
absolute = os.path.abspath('.')
american_file = os.path.join(absolute,american_file)
european_file = os.path.join(absolute,european_file)
# Renaming the file to European-Style format
shutil.move(american_file,european_file)