Making os.unlink() act like "rm -f"
Roy Smith
roy at panix.com
Sat Dec 11 15:47:09 EST 2010
In article <mailman.424.1292088328.2649.python-list at python.org>,
Christian Heimes <lists at cheimes.de> wrote:
> Am 11.12.2010 18:04, schrieb Roy Smith:
> > if os.access("file", os.F_OK):
> > os.unlink("file")
> >
> > but that's annoying too. What would people think about a patch to
> > os.unlink() to add an optional second parameter which says to ignore
> > attempts to remove non-existent files (just like "rm -f")? Then you
> > could do:
>
> -1
>
> os.unlink is a small wrapper around the unlink(2) function.
OK, fair enough. Perhaps a better place would be in a higher level
module like shutil.
It was suggested I look at shutil.rmtree(), but that only works of path
is a directory. Also, the meaning of the ignore_errors flag is not
quite what I'm looking for. I don't want to ignore errors, I just want
"if it doesn't exist, this is a no-op". In short, exactly what "rm -r"
does in the unix shell.
So, maybe a new function is shutils?
shutils.rm(path, force=False)
Delete the file at path. If force is True, this is a no-op if path does
not exist. Raises OSError if the operation fails.
More information about the Python-list
mailing list