Re: can you improve this text-only beginner copy program?
Grant Edwards via Python-list <[email protected]>
| Newsgroups | gmane.comp.python.general |
|---|---|
| Message-ID | <[email protected]> |
On 2025-08-27, Chris Angelico via Python-list <[email protected]> wrote: > On Thu, 28 Aug 2025 at 01:28, Ethan Carter <[email protected]> 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