Re: can you improve this text-only beginner copy program?

Roel Schroeven <[email protected]>
Newsgroups gmane.comp.python.general
Message-ID <[email protected]>
Op 28/08/2025 om 18:02 schreef MRAB:
> On 2025-08-28 03:08, Grant Edwards via Python-list wrote:
>>> 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.
>>
> Windows won't let you delete an open file.
> If an OS did let you delete an open file, how would you expect it to 
> behave? Would you still be able to use the file? Would the file be 
> marked for deletion and be deleted when it was finally closed?
Unix-like operating systems do let you delete an open file. It then 
disappears from directory listings, but is not actually deleted until 
the last process that has an open handle to the file closes that handle 
(or exits, in which case the handle is closed automatically).
>
> The Zen of Python says:
>
> In the face of ambiguity, refuse the temptation to guess.
>
> So, close it before attempting to delete it.
Oh yes, absolutely. An exception can be made for temporary files, I 
guess, if you can guarantee your code is only going to run on operating 
systems that support deleting open files: open a temporary file, 
immediately delete it, then the file doesn't appear in directory listing 
(or only for a very short time) and more importantly is guaranteed to be 
deleted even if your process terminates unexpectedly.

-- 
"Peace cannot be kept by force. It can only be achieved through understanding."
         -- Albert Einstein
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.