File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import json
44import os
5- import platform
65import tempfile
76from collections .abc import Generator
87from datetime import datetime
@@ -137,23 +136,24 @@ def _write_file_atomic(
137136 if append :
138137 content = read_file (path , encoding = encoding ) + content
139138 dirpath , _ = split_filepath (path )
140- auto_delete_temp_file = False if platform . system () == "Windows" else True
139+ temp_path = None
141140 try :
142141 with tempfile .NamedTemporaryFile (
143142 mode = mode ,
144143 dir = dirpath ,
145- delete = auto_delete_temp_file ,
144+ delete = False ,
146145 # delete_on_close=False, # supported since Python >= 3.12
147146 encoding = encoding ,
148147 ) as file :
149148 file .write (content )
150149 file .flush ()
151150 os .fsync (file .fileno ())
152151 temp_path = file .name
153- permissions = get_permissions (path ) if exists (path ) else None
154- os .replace (temp_path , path )
155- if permissions :
156- set_permissions (path , permissions )
152+ # file is now closed, safe to replace on Windows
153+ permissions = get_permissions (path ) if exists (path ) else None
154+ os .replace (temp_path , path )
155+ if permissions :
156+ set_permissions (path , permissions )
157157 except FileNotFoundError :
158158 # success - the NamedTemporaryFile has not been able
159159 # to remove the temp file on __exit__ because the temp file
You can’t perform that action at this time.
0 commit comments