File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed
Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change 55import json
66import os
77import pathlib
8+ import platform
89import re
910import shutil
1011import sys
@@ -1424,11 +1425,12 @@ def _write_file_atomic(
14241425 if append :
14251426 content = read_file (path , encoding = encoding ) + content
14261427 dirpath , _ = split_filepath (path )
1428+ auto_delete_temp_file = False if platform .system () == "Windows" else True
14271429 try :
14281430 with tempfile .NamedTemporaryFile (
14291431 mode = mode ,
14301432 dir = dirpath ,
1431- delete = True ,
1433+ delete = auto_delete_temp_file ,
14321434 # delete_on_close=False, # supported since Python >= 3.12
14331435 encoding = encoding ,
14341436 ) as file :
@@ -1445,6 +1447,11 @@ def _write_file_atomic(
14451447 # to remove the temp file on __exit__ because the temp file
14461448 # has replaced atomically the file at path.
14471449 pass
1450+ finally :
1451+ # attempt for fixing #121 (on Windows destroys created file on exit)
1452+ # manually delete the temporary file if still exists
1453+ if temp_path and exists (temp_path ):
1454+ remove_file (temp_path )
14481455
14491456
14501457def _write_file_non_atomic (
You can’t perform that action at this time.
0 commit comments