Skip to content

Commit 474f43e

Browse files
committed
Attempt for fixing #121.
1 parent 7d233d1 commit 474f43e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

fsutil/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import os
77
import pathlib
8+
import platform
89
import re
910
import shutil
1011
import 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

14501457
def _write_file_non_atomic(

0 commit comments

Comments
 (0)