-
Notifications
You must be signed in to change notification settings - Fork 0
/
archiving.py
49 lines (36 loc) · 1.32 KB
/
archiving.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
import os, sys, zipfile, datetime, schedule
def job():
print("Working")
path = "/home/nayeem/Desktop/texts"
log_file_extention = ".txt"
files = os.listdir(path);
now = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
archive_name = "log_" + now + ".zip"
try:
z = zipfile.ZipFile(os.path.join(path, archive_name), "w") # Creating zip file
except:
print("Error creating ZIP file")
sys.exit()
try :
# Adding files to archive
for file_name in os.listdir(path):
if file_name.endswith(log_file_extention):
print("Adding " + file_name)
file_path = os.path.join(path,file_name)
z.write(file_path, file_name) # Only adding the file instead of full directory
z.close() # Closing file after writing is finished
except:
print("Problem occurred while adding files to archive")
sys.exit()
try:
# Deleting archived files
for file_name in os.listdir(path):
if file_name.endswith(log_file_extention):
file_path = os.path.join(path,file_name)
os.remove(file_path)
except:
print("Problem occurred while deleting files")
sys.exit()
schedule.every(10).seconds.do(job)
while 1:
schedule.run_pending()