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: Make it possible to select return type for os.listdir
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: abacabadabacaba, r.david.murray, serhiy.storchaka, vstinner
Priority: normal Keywords:

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

Messages (7)
msg266093 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2016-05-22 18:08
Currently, os.listdir returns a list of strings, unless called with a bytes argument, in which case a list of byte strings is returned. I think that there should be a keyword argument to override this selection.
msg266098 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-22 18:52
You can just convert os.listdir() argument or result.

list(map(os.fsencode, os.listdir(strpath)))
list(map(os.fsdecode, os.listdir(bytespath)))
msg266154 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-05-23 15:02
The API for selecting the output type is to pass in the corresponding input type.  This is true for many functions in the python3 standard library.
msg266156 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2016-05-23 15:24
Unfortunately, this doesn't work if I pass a file descriptor.
msg266157 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-05-23 15:27
> Unfortunately, this doesn't work if I pass a file descriptor.

I strongly suggest you to handle filenames as Unicode ;-) It helps a lot on Windows, it's more convenient, etc.
msg266158 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2016-05-23 15:38
Unfortunately, on Linux, handling names as Unicode can cause some problems. For example, merely print()-ing such a name can cause UnicodeEncodeError.
msg266165 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-05-23 16:58
The issue of passing an fd is a fair point.

However, converting the filenames to bytes isn't going to help you print them, you'll just get decoding errors when converting them to strings instead of encoding errors.  You could use the surrogateescape error handler on stdout via PYTHONIONENCODING if you want to get the bytes out, but in that case you don't need to encode them to bytes yourself, your program can just handle them as strings and let the python IO machinery deal with the bytes.

If you really want bytes filenames from listdir with an fd, you can just apply os.fsencode to the results:  map(os.fsencode, listdir(fd))

So, I'm still -1 on this proposal.

(Note: there is an existing open issue about surrogateescape and subprocess binary streams, so that is a known problem we haven't solved yet, and represents a more general problem than this issue.)
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71272
2016-06-28 07:13:13serhiy.storchakasetstatus: open -> closed
resolution: rejected
2016-05-23 16:58:21r.david.murraysetmessages: + msg266165
2016-05-23 15:38:13abacabadabacabasetmessages: + msg266158
2016-05-23 15:28:00vstinnersetnosy: + vstinner
messages: + msg266157
2016-05-23 15:24:13abacabadabacabasetstatus: closed -> open
resolution: rejected -> (no value)
messages: + msg266156
2016-05-23 15:02:58r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg266154

resolution: rejected
stage: resolved
2016-05-22 18:52:36serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg266098
2016-05-22 18:08:24abacabadabacabacreate