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 5
5
import json
6
6
import os
7
7
import pathlib
8
+ import platform
8
9
import re
9
10
import shutil
10
11
import sys
@@ -1424,11 +1425,12 @@ def _write_file_atomic(
1424
1425
if append :
1425
1426
content = read_file (path , encoding = encoding ) + content
1426
1427
dirpath , _ = split_filepath (path )
1428
+ auto_delete_temp_file = False if platform .system () == "Windows" else True
1427
1429
try :
1428
1430
with tempfile .NamedTemporaryFile (
1429
1431
mode = mode ,
1430
1432
dir = dirpath ,
1431
- delete = True ,
1433
+ delete = auto_delete_temp_file ,
1432
1434
# delete_on_close=False, # supported since Python >= 3.12
1433
1435
encoding = encoding ,
1434
1436
) as file :
@@ -1445,6 +1447,11 @@ def _write_file_atomic(
1445
1447
# to remove the temp file on __exit__ because the temp file
1446
1448
# has replaced atomically the file at path.
1447
1449
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 )
1448
1455
1449
1456
1450
1457
def _write_file_non_atomic (
You can’t perform that action at this time.
0 commit comments