-
Notifications
You must be signed in to change notification settings - Fork 0
/
docsort.py
executable file
·60 lines (45 loc) · 1.68 KB
/
docsort.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
#!/usr/bin/env python
import os, sys, shutil, datetime
class DocSort:
def __init__(self, source_dir, target_dir, age_seconds):
for f in os.listdir(source_dir):
file_name = source_dir + '/' + f
if not os.path.isfile(file_name):
print file_name, 'is not a regular file'
continue
print 'Processing', file_name
mtime = int(os.stat(file_name).st_mtime)
date = datetime.datetime.fromtimestamp(mtime)
if (datetime.datetime.now() - date) < datetime.timedelta(0, age_seconds):
print 'Skipping', file_name, '(%s)' % (date, )
continue
dir = target_dir + '/' + '%04d' % (date.year, )
if not os.path.exists(dir):
os.makedirs(dir)
dir = dir + '/' + '%02d' % (date.month, )
if not os.path.exists(dir):
os.makedirs(dir)
try:
shutil.move(file_name, dir)
except:
e = sys.exc_info()[1]
print(str(e))
def usage():
print 'usage: %s source_dir target_dir <age_seconds>' % (sys.argv[0], )
sys.exit(2)
if __name__ == '__main__':
if len(sys.argv) < 3:
usage()
if len(sys.argv) > 3:
try:
age_seconds = int(sys.argv[3])
except:
usage()
else:
age_seconds = 2419200
source_dir = sys.argv[1]
target_dir = sys.argv[2]
for dir in [source_dir, target_dir]:
if not os.path.isdir(dir) or not os.access(dir, os.R_OK):
usage()
ds = DocSort(source_dir, target_dir, age_seconds)