-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathby_year.py
66 lines (54 loc) · 1.76 KB
/
by_year.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Move the files in the per-day subdirectories to a single directory for the
year.
"""
from __future__ import print_function
import argparse
import os.path
import re
import shutil
__author__ = 'mlg'
def getparser():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', type=int, default=1)
parser.add_argument('year')
return parser
def getargs():
parser = getparser()
args = parser.parse_args()
return args
def trace(level, msg):
if level <= _args.verbose:
print(msg)
def main(args):
names = os.listdir('.')
year = args.year
try:
os.mkdir(year)
except OSError as ex:
trace(1, 'Failed to make directory {}:\n{}'.format(year,
ex.strerror))
# Ok if directory already exists
for name in names:
m = re.match(year + '_(\d\d)_(\d\d)', name)
if not m:
trace(2, 'Skipping {} (No match)'.format(name))
continue
mmdd = m.group(1) + m.group(2)
subdir = os.listdir(name)
for subname in subdir:
if subname.startswith('.'):
trace(2, 'Skipping {} (Hidden File)'.format(os.path.join(name,
subname)))
continue
target = os.path.join(year, mmdd + '_' + subname)
if os.path.exists(target):
trace(2, 'Skipping {} (Target exists)'.format(
os.path.join(name, subname)))
continue
source = os.path.join(name, subname)
trace(1, 'Copying {} -> {}'.format(source, target))
shutil.copy(source, target)
if __name__ == '__main__':
_args = getargs()
main(_args)