Remove a file forcefuly as in "rm -f" or unlink a filepath from directory forcefully Remove a file forcefuly as in "rm -f" or unlink a filepath from directory forcefully linux linux

Remove a file forcefuly as in "rm -f" or unlink a filepath from directory forcefully


The --force option to rm means, to ignore non existing files and never prompt, according to my man page.

The never prompt part is easy, your python remove does not prompt, right?

The ignore non existing files is also easy: you could either check, if the file exists, right before you remove it. You have a small race condition, because the file might disappear between the existence check and the remove. Or you could catch the OSError, and verify that it is thrown because the file does not exist (OSError: [Errno 2] No such file or directory...). One other reason for the OSError is, that the file you want to remove is not a file but a directory.

The force option does mo permission magic (at least on my linux), just keep in mind, that removing a file is a write operation on the directory.