Issue 27086: Add closefd argument to os.listdir (original) (raw)

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.
History
Date User Action Args
2022-04-11 14:58:31 admin set github: 71273
2021-06-24 10:35:09 iritkatriel set status: open -> closednosy: + iritkatrielmessages: + resolution: rejectedstage: resolved
2016-05-23 15:06:20 r.david.murray set nosy: + r.david.murraymessages: +
2016-05-22 20:45:47 abacabadabacaba set messages: +
2016-05-22 18:48:19 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2016-05-22 18:15:32 abacabadabacaba create