Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > In win32 and linux platform, os modules has diffreent output order,is it a bug?

Reply
Thread Tools

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

 
 
Honghe Wu
Guest
Posts: n/a
 
      03-01-2013
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?
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      03-01-2013
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
 
Reply With Quote
 
 
 
 
Benjamin Kaplan
Guest
Posts: n/a
 
      03-01-2013
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.
 
Reply With Quote
 
Honghe Wu
Guest
Posts: n/a
 
      03-01-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" <> 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
>


 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      03-01-2013
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Big projects divided into modules and sub-modules ImpalerCore C Programming 0 03-10-2011 03:01 PM
Active State 64-bit and Win32 Modules pgodfrin Perl Misc 6 09-23-2009 03:24 PM
Win32 and modules pgodfrin Perl Misc 1 09-21-2009 08:42 PM
So much for the Linux Web Servers and Linux has got the server world. Duane Arnold Computer Support 25 06-27-2006 11:09 PM



Advertisments