-
Notifications
You must be signed in to change notification settings - Fork 3
/
file_management.py
30 lines (25 loc) · 897 Bytes
/
file_management.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
import glob
import os
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def cleanup(files, keep=500):
removed_files = []
for path in files[int(keep):]:
try:
os.remove(path)
removed_files.append(path)
except e:
print(e)
return removed_files
def get_list_of_img_path(path, reverse=False):
files = []
# Get a list of all files in the upload folder with allowed extension
for types in ALLOWED_EXTENSIONS:
paths = glob.glob(os.path.join(path, '*.{}'.format(types)))
paths = [os.path.abspath(path) for path in paths]
files.extend(paths)
# Sort the files by their creation date
files.sort(key=lambda x: os.path.getctime(x), reverse=reverse)
return files