-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_old_files.py
70 lines (48 loc) · 2.03 KB
/
move_old_files.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
67
68
69
70
import sys
import os
import time
import datetime
import shutil
def script_path():
path = os.path.realpath(os.path.dirname(sys.argv[0]))
os.chdir(path)
return path
def get_time(unix, file_form=False):
if file_form:
date = str(datetime.datetime.fromtimestamp(unix).strftime("%Y%m%d_%H%M%S"))
else:
date = str(datetime.datetime.fromtimestamp(unix).strftime("%Y-%m-%d %H:%M:%S"))
return date
def date_to_unix(date):
out = time.mktime(date.timetuple())
return out
def move_file(file, current_path, new_path):
currentFilePath = os.path.join(current_path, file)
newFilePath = os.path.join(new_path, file)
shutil.move(currentFilePath, newFilePath)
return True
def make_dir(new_dir):
''' make new dir and return new path '''
if not os.path.exists(new_dir):
os.makedirs(new_dir)
return new_dir
def main():
edge_time = date_to_unix(datetime.datetime(2019, 1, 1, 20, 10, 12)) # put some date here
dir_name = "older_than_" + get_time(edge_time, file_form=True)
current_path = script_path()
new_path = os.path.join(current_path, make_dir(dir_name))
files = [file for file in os.listdir()] # add some filter here
scriptName = os.path.basename(os.path.normpath(sys.argv[0]))
files.remove(scriptName) # don't move yourself :)
for file in files:
unix = os.path.getmtime(file)
fileTime = get_time(unix) # string format
if unix < edge_time: # check if file is older than edge_time
try:
move_file(file, current_path, new_path)
print("file moved: {}".format(file))
except:
print("failed to move file: {}".format(file))
return True
if __name__ == "__main__":
main()