Option to not raise if file does not exists for os.remove()?

What about a parameter, false by default, that suppresses the FileNotFoundError exception if true for os.remove()? Something similar to exist_ok for os.makedirs().

29.12.20 22:58, Marco Sulla пише:
What about a parameter, false by default, that suppresses the FileNotFoundError exception if true for os.remove()? Something similar to exist_ok for os.makedirs().
You have two options (and it's too much in my opinion):
try: os.remove(filename) except FileNotFoundError: pass
with contextlib.suppress(FileNotFoundError): os.remove(filename)

Hi Sergio,
The pathlib module includes this feature: https://docs.python.org/3/library/pathlib.html#pathlib.Path.unlink
Best,
Eelke
On Tue, Dec 29, 2020, 22:12 Serhiy Storchaka storchaka@gmail.com wrote:
29.12.20 22:58, Marco Sulla пише:
What about a parameter, false by default, that suppresses the FileNotFoundError exception if true for os.remove()? Something similar to exist_ok for os.makedirs().
You have two options (and it's too much in my opinion):
try: os.remove(filename) except FileNotFoundError: pass with contextlib.suppress(FileNotFoundError): os.remove(filename)
Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/TFNKPS... Code of Conduct: http://python.org/psf/codeofconduct/

On Tue, 29 Dec 2020 at 22:40, Eelke van den Bos eelkevdbos@gmail.com wrote:
Hi Sergio,
The pathlib module includes this feature: https://docs.python.org/3/library/pathlib.html#pathlib.Path.unlink
Best,
Eelke
I add that it's quite common to skip FileNotFoundError in removing a file. I think it's the same because exist_ok was added to os.makedirs().
Can I try: a PR?
participants (3)
-
Eelke van den Bos
-
Marco Sulla
-
Serhiy Storchaka