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

Discussion in 'Python' started by Honghe Wu, Mar 1, 2013.

  1. Honghe Wu

    Honghe Wu Guest

    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?
    Honghe Wu, Mar 1, 2013
    #1
    1. Advertising

  2. Honghe Wu

    Chris Rebert Guest

    Re: In win32 and linux platform, os modules has diffreent outputorder, is it a bug?

    On Fri, Mar 1, 2013 at 12:43 AM, Honghe Wu <> 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
    Chris Rebert, Mar 1, 2013
    #2
    1. Advertising

  3. Re: In win32 and linux platform, os modules has diffreent outputorder, is it a bug?

    On Fri, Mar 1, 2013 at 12:43 AM, Honghe Wu <> 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?
    > --


    The function doesn't specify a particular order, just that it will
    hand you a list of files. It grabs those from the underlying file
    system. It looks like Windows sorts it alphabetically and Linux just
    does whatever (maybe sorted by creation time?). I don't think it's a
    bug. If the order matters to you, sort it yourself.
    Benjamin Kaplan, Mar 1, 2013
    #3
  4. Honghe Wu

    Honghe Wu Guest

    Re: In win32 and linux platform, os modules has diffreent outputorder, is it a bug?

    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" <> wrote:

    > On Fri, Mar 1, 2013 at 12:43 AM, Honghe Wu <> 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
    >
    Honghe Wu, Mar 1, 2013
    #4
  5. Honghe Wu

    Nobody Guest

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

    On Fri, 01 Mar 2013 17:24:05 +0800, Honghe Wu wrote:

    > 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.


    You can sort the lists of files and subdirectories with e.g.:

    for root, dirs, files in os.walk(os.getcwd()):
    dirs[:] = sorted(dirs)
    files = sorted(files)
    ...

    Note that modifying the directory list in-place will affect which
    subdirectories are traversed and in what order.
    Nobody, Mar 1, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Soren Kuula
    Replies:
    2
    Views:
    486
    Soren Kuula
    Feb 1, 2004
  2. jcc
    Replies:
    15
    Views:
    4,682
    Nigel Wade
    May 12, 2006
  3. Mirek Fidler
    Replies:
    0
    Views:
    404
    Mirek Fidler
    Feb 22, 2004
  4. Coca
    Replies:
    15
    Views:
    630
    Alan Balmer
    Jan 14, 2004
  5. Bill Kelly
    Replies:
    6
    Views:
    335
    Bill Kelly
    Aug 27, 2004
Loading...

Share This Page