Skip to content
Permalink
Browse files

ensure directories when writing files

  • Loading branch information...
leifj committed Dec 2, 2019
1 parent bbff430 commit 90fc828383c5ed28c4746e75f3c8cbc45c05d603
Showing with 11 additions and 1 deletion.
  1. +11 −1 src/pyff/utils.py
@@ -289,10 +289,17 @@ def request_scheme(request):
return request.headers.get('X-Forwarded-Proto', request.scheme)


def safe_write(fn, data):
def ensure_dir(fn):
d = os.path.dirname(fn)
if not os.path.exists(d):
os.makedirs(d)


def safe_write(fn, data, mkdirs=False):
"""Safely write data to a file with name fn
:param fn: a filename
:param data: some string data to write
:param mkdirs: create directories along the way (False by default)
:return: True or False depending on the outcome of the write
"""
tmpn = None
@@ -306,6 +313,9 @@ def safe_write(fn, data):
else:
mode = 'w+b'

if mkdirs:
ensure_dir(fn)

if isinstance(data, six.binary_type):
data = data.decode('utf-8')

0 comments on commit 90fc828

Please sign in to comment.
You can’t perform that action at this time.