Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40235 > unrolled thread

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

Started byHonghe Wu <leopardsaga@gmail.com>
First post2013-03-01 00:43 -0800
Last post2013-03-01 12:56 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  In win32 and linux platform, os modules has diffreent output order, is it a bug? Honghe Wu <leopardsaga@gmail.com> - 2013-03-01 00:43 -0800
    Re: In win32 and linux platform, os modules has diffreent output order, is it a bug? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2013-03-01 01:01 -0800
    Re: In win32 and linux platform, os modules has diffreent output order, is it a bug? Chris Rebert <clp2@rebertia.com> - 2013-03-01 00:55 -0800
    Re: In win32 and linux platform, os modules has diffreent output order, is it a bug? Honghe Wu <leopardsaga@gmail.com> - 2013-03-01 17:24 +0800
      Re: In win32 and linux platform, os modules has diffreent output order, is it a bug? Nobody <nobody@nowhere.com> - 2013-03-01 12:56 +0000

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

FromHonghe Wu <leopardsaga@gmail.com>
Date2013-03-01 00:43 -0800
SubjectIn win32 and linux platform, os modules has diffreent output order, is it a bug?
Message-ID<270cf08f-aefd-436c-a3e6-4adf373d5356@googlegroups.com>
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?

[toc] | [next] | [standalone]


#40237

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2013-03-01 01:01 -0800
Message-ID<mailman.2709.1362128671.2939.python-list@python.org>
In reply to#40235
On Fri, Mar 1, 2013 at 12:43 AM, Honghe Wu <leopardsaga@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?
> --

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.

[toc] | [prev] | [next] | [standalone]


#40238

FromChris Rebert <clp2@rebertia.com>
Date2013-03-01 00:55 -0800
Message-ID<mailman.2710.1362129542.2939.python-list@python.org>
In reply to#40235
On Fri, Mar 1, 2013 at 12:43 AM, Honghe Wu <leopardsaga@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

[toc] | [prev] | [next] | [standalone]


#40239

FromHonghe Wu <leopardsaga@gmail.com>
Date2013-03-01 17:24 +0800
Message-ID<mailman.2711.1362129847.2939.python-list@python.org>
In reply to#40235

[Multipart message — attachments visible in raw view] — view raw

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@rebertia.com> wrote:

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

[toc] | [prev] | [next] | [standalone]


#40248

FromNobody <nobody@nowhere.com>
Date2013-03-01 12:56 +0000
Message-ID<pan.2013.03.01.12.56.50.232000@nowhere.com>
In reply to#40239
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web