In win32 and linux platform, os modules has diffreent output order, is it a bug?

Honghe Wu leopardsaga at gmail.com
Fri Mar 1 04:24:05 EST 2013


Thanks! Cause I need sorted returnd list, and the arbitrary list makes the
other procedure go wrong. Maybe the I/O speed is more important in other
cases.
On Mar 1, 2013 4:55 PM, "Chris Rebert" <clp2 at rebertia.com> wrote:

> On Fri, Mar 1, 2013 at 12:43 AM, Honghe Wu <leopardsaga at gmail.com> wrote:
> > env: python 2.7.3
> >
> > 6 test files' name in a directory as below:
> > 12ab  Abc  Eab  a1bc  acd  bc
> >
> > the following is test code:
> > for root, dirs, files in os.walk(os.getcwd()):
> >     print files
> >
> > the output in win32 platform is:
> > ['12ab', 'a1bc', 'Abc', 'acd', 'bc', 'Eab']
> >
> > but in linux is:
> > ['Eab', 'acd', 'a1bc', '12ab', 'bc', 'Abc' ]
> >
> > they are so different. a bug?
>
> Nope. When os.walk() fetches a listing of the contents of a directory,
> it internally uses os.listdir() (or a moral equivalent thereof). The
> docs for os.listdir() state that "The [returned] list is in arbitrary
> order.". The order is dependent on the OS and filesystem, and likely
> also more obscure factors (e.g. the order in which the files were
> created). The lack of any required ordering allows for improved I/O
> performance in many/most cases.
>
> Cheers,
> Chris
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130301/2c4b66a3/attachment.html>


More information about the Python-list mailing list