import os
import shutil
import time
watched_path = '/data/tmp/'
#watched_path = '/tmp/'
target_dir = '/tmp/guyu/'
watched_file = 'index-d-'
watched_files = {}
for file in os.listdir(watched_path):
if file.startswith(watched_file):
watched_files[file] = None
for k in watched_files.keys():
watched_files[k] = os.path.getmtime(''.join([watched_path, k]))
while True:
time.sleep(60)
print('check files')
saved_files = set(watched_files.keys())
current_files = []
for file in os.listdir(watched_path):
if file.startswith(watched_file):
current_files.append(file)
erase = list(saved_files - set(current_files))
for file in erase:
if file in watched_files:
print('delete',file)
del watched_files[file]
diff = list(set(current_files) - saved_files)
if diff:
print('diff =',diff)
for file in diff:
watched_files[file] = 0
try:
for file in watched_files.keys():
current_modified_time = os.path.getmtime(''.join([watched_path, file]))
#print('modifytime',current_modified_time, watched_files[file])
test_time = current_modified_time - watched_files[file]
if current_modified_time - watched_files[file] > 0.1:
target_file = os.path.join(target_dir, '{}_{}'.format(file, time.strftime('%Y%m%d%H%M%S')))
shutil.copy2(''.join([watched_path, file]), target_file)
watched_files[file] = current_modified_time
print('copy', target_file)
except Exception as e:
print(e)