X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Futils.py;h=bfe88b40bac05eea67d065953707c88549a190b6;hb=1394646a0a531f6b13a4af7b1e3eec9951a6f9fb;hp=94b496dd0a693ae61997881458e1e202a184a330;hpb=92120217eb27d820bfba0f6a50837f111cbf80ef;p=youtube-dl diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 94b496dd0..bfe88b40b 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -71,10 +71,10 @@ 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): + if sys.version_info < (3, 0) and sys.platform != 'win32': encoding = get_filesystem_encoding() # os.path.basename returns a bytes object, but NamedTemporaryFile # will fail if the filename contains non ascii characters unless we @@ -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: