From e07cb5a03cb480a9d645f1cc8aa15bd4f5034b35 Mon Sep 17 00:00:00 2001 From: Sri Konduru Date: Mon, 18 Apr 2016 20:34:25 -0500 Subject: [PATCH] use os.replace instead of os.rename if available --- notebook/services/contents/fileio.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/notebook/services/contents/fileio.py b/notebook/services/contents/fileio.py index e4b6b9c14..5d7b3de78 100644 --- a/notebook/services/contents/fileio.py +++ b/notebook/services/contents/fileio.py @@ -30,6 +30,16 @@ except ImportError: #PY2 from base64 import encodestring as encodebytes, decodestring as decodebytes +def replace_file(src, dst): + """ replace dst with src + + switches between os.replace or os.rename based on python 2.7 or python 3 + """ + try: + os.replace(src, dst) + except: + os.rename(src, dst) + def copy2_safe(src, dst, log=None): """copy src to dst @@ -104,7 +114,7 @@ def atomic_writing(path, text=True, encoding='utf-8', log=None, **kwargs): if os.name == 'nt' and os.path.exists(path): # Rename over existing file doesn't work on Windows os.remove(path) - os.rename(tmp_path, path) + replace_file(tmp_path, path) raise # Flush to disk @@ -277,8 +287,8 @@ class FileManagerMixin(Configurable): # Rename over existing file doesn't work on Windows if os.name == 'nt' and os.path.exists(invalid_file): os.remove(invalid_file) - os.rename(os_path, invalid_file) - os.rename(tmp_path, os_path) + replace_file(os_path, invalid_file) + replace_file(tmp_path, os_path) return self._read_notebook(os_path, as_version) def _save_notebook(self, os_path, nb):