import
os
import
threading
import
time
def
get_file_list(file_path):
dir_list
=
os.listdir(file_path)
if
not
dir_list:
return
else
:
dir_list
=
sorted
(dir_list, key
=
lambda
x: os.path.getmtime(os.path.join(file_path, x)))
return
dir_list
def
get_size(file_path):
totalsize
=
0
for
filename
in
os.listdir(file_path):
totalsize
=
totalsize
+
os.path.getsize(os.path.join(file_path, filename))
return
totalsize
/
1024
/
1024
def
detect_file_size(file_path, size_Max, size_Del):
print
(get_size(file_path))
if
get_size(file_path) > size_Max:
fileList
=
get_file_list(file_path)
for
i
in
range
(
len
(fileList)):
if
get_size(file_path) > (size_Max
-
size_Del):
print
(
"del :%d %s"
%
(i
+
1
, fileList[i]))
def
detectFileSize():
while
True
:
print
(
'======detect============'
)
detect_file_size(
"/Users/aaron/Downloads/"
,
100
,
30
)
time.sleep(
5
)
if
__name__
=
=
"__main__"
:
detect_thread
=
threading.Thread(target
=
detectFileSize)
detect_thread.start()