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: os.listdir on Linux returns empty list on some errors
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, gstiehr
Priority: normal Keywords:

Created on 2006-04-10 20:12 by gstiehr, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg28198 - (view) Author: Gary Stiehr (gstiehr) Date: 2006-04-10 20:12
os.listdir() (defined in the posix_listdir() function
on line 1449 of Modules/posixmodule.c in the
Python-2.4.3 source distribution) does not check for an
error condition when it calls the readdir() system call
on line 1665 of posixmodule.c.  

According to the readdir(3) man page included the
Scientific Linux 4.2 (based off of Red Hat Enterprise
Linux 4 sources):

The  readdir()  function  returns  a pointer to a
dirent structure, or NULL if an error occurs or
end-of-file is reached.


It seems that the posix_listdir() function assumes that
NULL can only mean end-of-file.  Therefore, in the
situation where readdir() returns NULL due to some
error, such as an I/O Error, posix_listdir() ends the
while loop started at line 1665 and goes to the
closedir() call at line 1702.  This results in an
os.listdir() call returning an empty list (as if the
directory had no contents) instead of raising an exception.

This error was noticed because we performed an ls in a
particular directory and it returned with an I/O error.
 I was then writing a python script to monitor for this
condition when I found that the os.listdir() in this
directory returned an empty list instead of an I/O error.

This behavior was noticed using Python 2.3.4; I looked
at the latest (as of 2006-04-07) stable release source
(Python 2.4.3) to investigate and to provide the
details in this bug report.  I also took a quick look
at the most recent 2.5.x code and although the
posix_listdir function has changed, it still appears as
if it would return an empty list if the readdir() call
returns NULL.
msg28199 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-04-11 06:51
Logged In: YES 
user_id=849994

Thank you for the report, fixed in rev. 45259, 45260.
History
Date User Action Args
2022-04-11 14:56:16adminsetgithub: 43187
2006-04-10 20:12:34gstiehrcreate