I am trying to understand how the underlying process differs if I call the os.listdir() function as compared to running ls in the terminal.
After much searching, I came across these 2 keypoints GNU ls waits until all entries are fetched before returning any of them. Python listing functions do not necessarily invoke GNU ls.
So,I want to find out if the python listing functions also exhibit similar behavior like waiting for all entries before returning any of them or return the entries as received.
os.listdircannot return entries as it reads them.returndoesn't work like that - a function can only return once, and it ends as soon as it returns.os.listdirreturns a list of names, and it cannot add more data to that list after it returns.For
os.listdirto not output all its data at once, it would have to be designed differently. For example, it could return an iterator (likeos.scandirdoes), and the iterator could read one directory entry on every iteration. Or it could return aqueue.Queueand start a background thread that reads directory entries andputs their names on the queue one by one.os.listdirwas not designed like that. It was designed to read directory entries, put their names in a list, and return that list.