Order in which files are read using os.listdir? [duplicate]
Asked Answered
D

2

16

When performing the following code, is there an order in which Python loops through files in the provided directory? Is it alphabetical? How do I go about establishing an order these files are loops through, either by date created/modified or alphabetically).

import os
for file in os.listdir(path)
    df = pd.read_csv(path+file)
    // do stuff
Dimitri answered 13/6, 2017 at 22:33 Comment(1)
os.listdir(path) : Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries '.' and '..' even if they are present in the directory.Wiatt
O
30

You asked several questions:

  • Is there an order in which Python loops through the files?

No, Python does not impose any predictable order. The docs say 'The list is in arbitrary order'. If order matters, you must impose it. Practically speaking, the files are returned in the same order used by the underlying operating system, but one mustn't rely on that.

  • Is it alphabetical?

Probably not. But even if it were you mustn't rely upon that. (See above).

  • How could I establish an order?

for file in sorted(os.listdir(path)):

Occupational answered 13/6, 2017 at 22:39 Comment(4)
-1. As jOOsko points out, the docs say 'arbitrary order'. arbitrary != os order. This is also my experience, that you cannot rely on, or expect, os order. If order matters, you must impose it.Izanami
Thanks, @MalikA.Rumi. I appreciate the assist.Stutsman
"same order used by the underlying operating system" does this imply sequential IO?Oahu
No, I did not intend to imply anything about sequential IO.Stutsman
S
3

As per documentation: "The list is in arbitrary order"

https://docs.python.org/3.6/library/os.html#os.listdir

If you wish to establish an order (alphabetical in this case), you could sort it.

import os
for file in sorted(os.listdir(path)):
    df = pd.read_csv(path+file)
    // do stuff
Selfsustaining answered 13/6, 2017 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.