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: Add dir_fd and follow_symlinks kwargs to os.listdir and os.scandir
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: abacabadabacaba, serhiy.storchaka
Priority: normal Keywords:

Created on 2016-05-22 17:53 by abacabadabacaba, last changed 2022-04-11 14:58 by admin.

Messages (5)
msg266091 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2016-05-22 17:53
Many functions in os module support dir_fd and follow_symlinks keyword arguments. I think that os.listdir and os.scandir should do likewise.

See also: issue25996.
msg266099 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-22 18:59
os.listdir() with dir_fd:

    fd = os.open(path, os.O_RDONLY, dir_fd=dir_fd)
    try:
        result = os.listdir(fd)
    finally:
        os.close(fd)

What meaning of follow_symlinks for os.listdir()?
msg266105 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2016-05-22 20:35
1. Yes, it's possible to emulate dir_fd this way, so this is just for convenience.
2. If follow_symlinks is False, O_NOFOLLOW is passed to the underlying open(2) syscall. Of course, this doesn't make sense if a file descriptor is passed already.
msg266109 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-22 21:14
But why you want to pass O_NOFOLLOW to the underlying open(2) syscall?
msg266113 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2016-05-22 22:25
Mostly for consistency with other functions. Also, this provides an easy way to walk a directory tree recursively: just call listdir on every member, and if it doesn't raise OSError, that member must be a directory. With follow_symlinks=False, this method wouldn't follow symlinks, unlike os.walk and os.fwalk, which always follow symlinks at least to differentiate between symlinks to directories and other symlinks.
History
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71271
2016-05-22 22:25:35abacabadabacabasetmessages: + msg266113
2016-05-22 21:14:33serhiy.storchakasetmessages: + msg266109
2016-05-22 20:35:57abacabadabacabasetmessages: + msg266105
2016-05-22 18:59:49serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg266099
2016-05-22 17:53:26abacabadabacabacreate