This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Path.unlink should have a missing_ok parameter
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: lordmauve, miss-islington, pitrou, rbu, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-03-22 17:56 by rbu, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6191 merged python-dev, 2018-03-22 18:00
Messages (4)
msg314277 - (view) Author: rbu (rbu) * Date: 2018-03-22 17:56
Similarly to how several pathlib file creation functions have an "exists_ok" parameter, we should introduce "missing_ok" that makes removal functions not raise an exception when a file or directory is already absent.

IMHO, this should cover Path.unlink and Path.rmdir.

Note, Path.resolve() has a "strict" parameter since 3.6 that does the same thing. Naming this of this new parameter tries to be consistent with the "exists_ok" parameter as that is more explicit about what it does (as opposed to "strict").
msg321822 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-07-17 13:10
It can be written as

    try:
        path.inlink()
    except FileNotFoundError:
        pass

If you want to save few lines of code, you can use contextlib.suppress().

    with suppress(FileNotFoundError): path.inlink()

I suggest to close this issue. It is better to keep the API simple and orthogonal. Adding an option in Path.unlink() will require adding this support of this option in third-part implementations of Path. In general, adding a single boolean parameter is not considered a good practice in Python.

A "strict" parameter in Path.resolve() does the different thing. In both cases Path.resolve() returns a value, and you can't implement strict=False by catching exception externally.
msg321831 - (view) Author: Daniel Pope (lordmauve) * Date: 2018-07-17 14:07
This would be a shortcut in the common case that you simply want an idempotent "make sure this file/symlink is gone" operation.

There are already boolean options to enable idempotent behaviour in several pathlib implementations, such as mkdir(exist_ok=True) and touch(exist_ok=True). write_bytes() and write_text() are also idempotent. unlink() aligns well with this.

Because this operation doesn't exist, developers are tempted to write

    if path.exists():
        path.unlink()

which both has a TOCTTOU bug and doesn't correctly handle symlinks.
msg342605 - (view) Author: miss-islington (miss-islington) Date: 2019-05-15 22:02
New changeset d9e006bcefe6fac859b1b5d741725b9a91991044 by Miss Islington (bot) (‮zlohhcuB treboR) in branch 'master':
bpo-33123: pathlib: Add missing_ok parameter to Path.unlink (GH-6191)
https://github.com/python/cpython/commit/d9e006bcefe6fac859b1b5d741725b9a91991044
History
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77304
2019-05-15 22:06:00pitrousetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-15 22:02:14miss-islingtonsetnosy: + miss-islington
messages: + msg342605
2018-07-17 14:07:41lordmauvesetmessages: + msg321831
2018-07-17 13:10:28serhiy.storchakasetnosy: + serhiy.storchaka, pitrou
messages: + msg321822
2018-07-17 11:01:21lordmauvesetnosy: + lordmauve
2018-03-22 18:00:58python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request5938
2018-03-22 17:56:36rbucreate