sort a list of files
Scott David Daniels
scott.daniels at acm.org
Sat May 6 19:13:11 EDT 2006
Ryan Forsythe wrote:
> Gary Wessle wrote:
>> Hi
>>
>> I am trying to print out the contents of a directory, sorted.
> ...
>> if I remove ".sort()" at the end of line 6 I get an unsorted list of
>> files, if I leave it I get None. who do I fix this?
>
> `blah.sort()` sorts in-place and returns None. You probably want
> sorted(blah):
>
> >>> a = [3, 1, 4, 1, 5, 9]
> >>> sorted(a)
> [1, 1, 3, 4, 5, 9]
> >>> a
> [3, 1, 4, 1, 5, 9]
> >>> a.sort()
> >>> a
> [1, 1, 3, 4, 5, 9]
If you are using an old version of Python (2.3.X or before),
the you just need to break your statement up:
Instead of:
6 print os.listdir(sys.argv[1]).sort()
use:
6 files = os.listdir(sys.argv[1])
7 files.sort()
8 print files
--Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list