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.

Title: Add closefd argument to os.listdir
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: abacabadabacaba, iritkatriel, r.david.murray, serhiy.storchaka
Priority: normal Keywords:

Created on 2016-05-22 18:15 by abacabadabacaba, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg266094 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2016-05-22 18:15
Currently, when given a file descriptor, os.listdir will duplicate it so that the original file descriptor is not closed. In many cases, a file descriptor is not needed anymore after directory is listed, so this is not necessary. I propose adding a keyword argument closefd to os.listdir which, if set to True, will make os.listdir take ownership of the passed file descriptor and close it at the end.
msg266097 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-22 18:48
What is the problem with closing a file descriptor after calling os.listdir()?
msg266106 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2016-05-22 20:45
Not a problem, just two unnecessary syscalls. Also, I think that many of those who pass a file descriptor to os.listdir don't need it afterwards, because after you fstat() a file descriptor (to discover that it points to a directory) and read the directory contents, there are few things you can use it for.
msg266155 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-05-23 15:06
This proposal doesn't feel to me like it matches the general APIs we have in the standard library.  I would think the python style for this would be to use a context manager when opening the fd.  So, I'm -1 on this proposal.
msg396468 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-24 10:35
I agree with Serhiy and David. The responsibility for closing the file is with whoever opened it, and adding an option in a function for it to also close the fd after doing what it was designed to do makes the API unnecessarily over-complicated.
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71273
2021-06-24 10:35:09iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg396468

resolution: rejected
stage: resolved
2016-05-23 15:06:20r.david.murraysetnosy: + r.david.murray
messages: + msg266155
2016-05-22 20:45:47abacabadabacabasetmessages: + msg266106
2016-05-22 18:48:19serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg266097
2016-05-22 18:15:32abacabadabacabacreate