On 2025-08-27, Chris Angelico via Python-list <python-list@python.org> wrote: > On Thu, 28 Aug 2025 at 01:28, Ethan Carter <ec1...@somewhere.edu> wrote: >> def copy(s, d): >> """Copies text file named S to text file named D.""" >> with open(s) as src: >> with open(d, "w") as dst: >> try: >> dst.write(src.read()) >> except Exception: >> os.remove(d) >> raise >> > > In the event of an exception, you attempt to remove the destination > file BEFORE exiting the `with` statement. While that might succeed on > some platforms, it will potentially fail on others.
Specifically, it should work on Unix/Linux OSes. I _think_ it will fail on (most) Windows OSes, but making definitive statements about how things work on "Windows" is beyond my ken. -- Grant -- https://mail.python.org/mailman3//lists/python-list.python.org