Issue 33123: Path.unlink should have a missing_ok parameter (original) (raw)

Issue33123

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:58 admin set github: 77304
2019-05-15 22:06:00 pitrou set status: open -> closedresolution: fixedstage: patch review -> resolved
2019-05-15 22:02:14 miss-islington set nosy: + miss-islingtonmessages: +
2018-07-17 14:07:41 lordmauve set messages: +
2018-07-17 13:10:28 serhiy.storchaka set nosy: + serhiy.storchaka, pitroumessages: +
2018-07-17 11:01:21 lordmauve set nosy: + lordmauve
2018-03-22 18:00:58 python-dev set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest5938>
2018-03-22 17:56:36 rbu create