473,386 Members | 1,702 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,386 software developers and data experts.

Help sorting a list by file extension

Trying to operate on a list of files similar to this:

test.1
test.2
test.3
test.4
test.10
test.15
test.20

etc.

I want to sort them in numeric order instead of string order. I'm starting with
this code:

import os

for filename in [filename for filename in os.listdir(os.getcwd())]:
print filename
#Write to file, but with filenames sorted by extension
Desired result is a file containing something like:
C:\MyFolder\test.1,test.001
C:\MyFolder\test.2,test.002
C:\MyFolder\test.3,test.003
C:\MyFolder\test.4,test.004
C:\MyFolder\test.10,test.010
C:\MyFolder\test.15,test.015
C:\MyFolder\test.20,test.020

I need to order by that extension for the file output.

I know I've got to be missing something pretty simple, but am not sure what.
Does anyone have any ideas on what I'm missing?

Thanks.

-Pete Schott
Aug 12 '05 #1
4 4700
On Fri, 12 Aug 2005 00:06:17 GMT, Peter A. Schott <pa******@no.yahoo.spamm.com> wrote:
Trying to operate on a list of files similar to this:

test.1
test.2
test.3
test.4
test.10
test.15
test.20

etc.

I want to sort them in numeric order instead of string order. I'm starting with
this code:

import os

for filename in [filename for filename in os.listdir(os.getcwd())]:
print filename
#Write to file, but with filenames sorted by extension
Desired result is a file containing something like:
C:\MyFolder\test.1,test.001
C:\MyFolder\test.2,test.002
C:\MyFolder\test.3,test.003
C:\MyFolder\test.4,test.004
C:\MyFolder\test.10,test.010
C:\MyFolder\test.15,test.015
C:\MyFolder\test.20,test.020

I need to order by that extension for the file output.

I know I've got to be missing something pretty simple, but am not sure what.
Does anyone have any ideas on what I'm missing?

Thanks.

Decorate with the integer value, sort, undecorate. E.g.,
namelist = """\ ... test.1
... test.2
... test.3
... test.4
... test.10
... test.15
... test.20
... """.splitlines() namelist ['test.1', 'test.2', 'test.3', 'test.4', 'test.10', 'test.15', 'test.20']

Just to show we're doing something namelist.reverse()
namelist ['test.20', 'test.15', 'test.10', 'test.4', 'test.3', 'test.2', 'test.1']

this list comprehension makes a sequence of tuples like (20, 'test.20'), (15, 'test.15') etc.
and sorts them, and then takes out the name from the sorted (dec, name) tuple sequence.
[name for dec,name in sorted((int(nm.rsplit('.',1)[1]),nm) for nm in namelist)] ['test.1', 'test.2', 'test.3', 'test.4', 'test.10', 'test.15', 'test.20']

The lexical sort, for comparison: sorted(namelist) ['test.1', 'test.10', 'test.15', 'test.2', 'test.20', 'test.3', 'test.4']

This depends on the extension being nicely splittable with a single '.', but that
should be the case for you I think, if you make sure you eliminate directory names
and file names that don't end that way. You can look before you leap or catch the
conversion exceptions, but to do that, you'll need a loop instead of a listcomprehension.
[name for dec,name in sorted((int(nm.split('.')[1]),nm) for nm in namelist)] ['test.1', 'test.2', 'test.3', 'test.4', 'test.10', 'test.15', 'test.20'] sorted(namelist)

['test.1', 'test.10', 'test.15', 'test.2', 'test.20', 'test.3', 'test.4']

Regards,
Bengt Richter
Aug 12 '05 #2
Bengt Richter wrote:
[name for dec,name in sorted((int(nm.split('.')[1]),nm) for nm in namelist)]
['test.1', 'test.2', 'test.3', 'test.4', 'test.10', 'test.15', 'test.20']


Giving a key argument to sorted will make it simpler::
sorted(namelist, key=lambda x:int(x.rsplit('.')[-1]))


-- george
Aug 12 '05 #3
On Fri, 12 Aug 2005, Bengt Richter wrote:
On Fri, 12 Aug 2005 00:06:17 GMT, Peter A. Schott <pa******@no.yahoo.spamm.com> wrote:
Trying to operate on a list of files similar to this:

test.1
test.2
test.3

I want to sort them in numeric order instead of string order.

[name for dec,name in sorted((int(nm.rsplit('.',1)[1]),nm) for nm in namelist)] ['test.1', 'test.2', 'test.3', 'test.4', 'test.10', 'test.15', 'test.20']

This depends on the extension being nicely splittable with a single '.'


You could use os.path.splitext to do that more robustly:
[name for dec,name in sorted((int(os.path.splitext(nm)[1][1:]),nm) for nm in namelist)]


tom

--
Everybody with a heart votes love
Aug 12 '05 #4
OK - I actually got something working last night with a list that is then
converted into a dictionary (dealing with small sets of data - < 200 files per
run). However, I like the sorted list option - I didn't realize that was even
an option within the definition and wasn't quite sure how to get there. I
realized I could use os.path.splitext and cast that to an int, but was having
trouble with the sort.

My files only have a single "." in them so this will work well for me.

(from Tom's code)
[name for dec,name in
sorted((int(os.path.splitext(nm)[1][1:]),nm) for nm in namelist)]

I'll give that a try - it would eliminate the dictionary part of my code and be
a little more efficient.

Thanks to all for the quick responses.

-Pete

Peter A. Schott <pa******@no.yahoo.spamm.com> wrote:
Trying to operate on a list of files similar to this:

test.1
test.2
test.3
test.4
test.10
test.15
test.20

etc.

I want to sort them in numeric order instead of string order. I'm starting with
this code:

import os

for filename in [filename for filename in os.listdir(os.getcwd())]:
print filename
#Write to file, but with filenames sorted by extension
Desired result is a file containing something like:
C:\MyFolder\test.1,test.001
C:\MyFolder\test.2,test.002
C:\MyFolder\test.3,test.003
C:\MyFolder\test.4,test.004
C:\MyFolder\test.10,test.010
C:\MyFolder\test.15,test.015
C:\MyFolder\test.20,test.020

I need to order by that extension for the file output.

I know I've got to be missing something pretty simple, but am not sure what.
Does anyone have any ideas on what I'm missing?

Thanks.

-Pete Schott

Aug 12 '05 #5

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

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
5
by: SStory | last post by:
Hi all, I really needed to get the icons associated with each file that I want to show in a listview. I used the follow modified code sniplets found on the internet. I have left in...
6
by: Rylios | last post by:
I am trying to make a very basic text editor using a linked list, fstream, sorting... This is what i have so far...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.