-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.py
83 lines (58 loc) · 2.05 KB
/
files.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import json
import pickle
from random import randint
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.contrib.staticfiles import finders
class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name, max_length=None):
"""
Returns a filename that's free on the target storage system, and
available for new content to be written to.
This file storage solves overwrite on upload problem.
Found at http://djangosnippets.org/snippets/976/
"""
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
return name
def upload_pic(uploaded_file, to_file):
destination = open(os.path.join(settings.MEDIA_ROOT, to_file), 'wb')
for chunk in uploaded_file.chunks():
destination.write(chunk)
destination.close()
def read_all(path):
with open(path) as f:
return f.read()
def read_lines(f, remove_empty=False):
lines = f.read().splitlines()
lines = [line.strip() for line in lines]
if remove_empty: lines = [s for s in lines if s]
return lines
def get_extension(filename):
parts = filename.split('.')
return parts[-1].lower()
def random_filename(prefix, extension):
while True:
out_file = '%s_%d.%s' % (prefix, randint(0, 1000000), extension)
if not os.path.exists(out_file):
return out_file
def read_static_file(path):
path = finders.find(path)
with open(path) as f:
return f.read()
def get_extension(filename):
ext = filename.rsplit('.', maxsplit=1)
return ext[1].lower() if len(ext) == 2 else None
def quick_write_json(filename, data):
with open(filename, 'w') as f:
f.write(json.dumps(data))
def quick_write_bin(filename, data):
with open(filename, 'wb') as f:
pickle.dump(data, f)
def quick_read_json(filename):
with open(filename) as f:
return json.loads(f.read())
def quick_read_bin(filename):
with open(filename, 'rb') as f:
return pickle.load(f)