summary refs log tree commit diff
diff options
context:
space:
mode:
authorIvan Kozik <ivan@ludios.org>2014-11-20 06:05:39 +0000
committerIvan Kozik <ivan@ludios.org>2014-11-20 06:26:34 +0000
commit1394646a0a531f6b13a4af7b1e3eec9951a6f9fb (patch)
tree5f58d06f15d31a8e7b522401a3ed1c30ca06c7e1
parent61ee5aeb7363d16c16cdec0914ade78af1afd8fd (diff)
downloadyoutube-dl-1394646a0a531f6b13a4af7b1e3eec9951a6f9fb.tar.gz
youtube-dl-1394646a0a531f6b13a4af7b1e3eec9951a6f9fb.tar.xz
youtube-dl-1394646a0a531f6b13a4af7b1e3eec9951a6f9fb.zip
Fix "ERROR: Cannot write metadata to JSON file" on Windows
Fixes #4246
-rw-r--r--youtube_dl/utils.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index f339fcb31..bfe88b40b 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -71,7 +71,7 @@ def preferredencoding():
 
 
 def write_json_file(obj, fn):
-    """ Encode obj as JSON and write it to fn, atomically """
+    """ Encode obj as JSON and write it to fn, atomically if possible """
 
     fn = encodeFilename(fn)
     if sys.version_info < (3, 0) and sys.platform != 'win32':
@@ -108,6 +108,13 @@ def write_json_file(obj, fn):
     try:
         with tf:
             json.dump(obj, tf)
+        if sys.platform == 'win32':
+            # Need to remove existing file on Windows, else os.rename raises
+            # WindowsError or FileExistsError.
+            try:
+                os.unlink(fn)
+            except OSError:
+                pass
         os.rename(tf.name, fn)
     except:
         try: