473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sort a list of files

Hi

I am trying to print out the contents of a directory, sorted.

**************** the code ****************
1 import os, sys
2
3 if len(sys.argv) < 2:
4 sys.exit("please enter a suitable directory.")
5
6 print os.listdir(sys.argv[1]).sort()
************************************************** **************

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?

thank you
May 6 '06 #1
2 1554
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]

--
Ryan Forsythe
May 6 '06 #2
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
sc***********@acm.org
May 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Zupan | last post by:
I have a list that includes files and directories ie: list = I want to sort it so it looks like this I'm just wondering if there is an easy way to do this
2
by: Jean-Paul Lauque | last post by:
Hello, I'm beginning in the ASP world... I would like to sort (descending) list of files in directory. Parameter is directory url. How I can to do that with ASP not ASP.NET.
4
by: Hrvoje Vrbanc | last post by:
Hello all! I have a small problem: I have an ASP.NET page that displays content of a folder in a DataGrid (more precisely, it displays Name, Lenth and CreationDate of the files). Default sort...
10
by: Amit | last post by:
Hi I'm having a problem using arraylist.sort. I'm using FileInfo.GetFiles() to receive a list of files from a directory. The files have a sequential number to start then theres either an "A"...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
18
by: xahlee | last post by:
Last year, i've posted a tutorial and commentary about Python and Perl's sort function. (http://xahlee.org/perl-python/sort_list.html) In that article, i discussed a technique known among...
4
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
4
by: jonathan184 | last post by:
Hi I have a perl script, basically what it is suppose to do is check a folder with files. Now the files are checked using a timestamp with the command ls -l so the timestamp in this format is...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.