-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOldFileGone.py
More file actions
33 lines (23 loc) · 841 Bytes
/
OldFileGone.py
File metadata and controls
33 lines (23 loc) · 841 Bytes
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
import os
import time
def main():
dir_path = r"YourDirPath"
hour = 3 #int
loop_sleep_time = 1 #int
while 1:
delete_file(dir_path, hour)
time.sleep(hour_to_Second(loop_sleep_time))
def delete_file(dir_path, hour):
for file_name in os.listdir(dir_path):
file_path = os.path.join(dir_path, file_name)
if os.path.isfile(file_path):
# get the current time ==> time.time() & creation_time ==> os.path.getctime(file_path)
time_difference = time.time() - os.path.getctime(file_path)
if time_difference > hour_to_Second(hour):
os.remove(file_path)
print(f"File {file_name} deleted.")
else:
print(f"File {file_name} is not old enough.")
def hour_to_Second(hour):
return hour * 3600
main()