471,344 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Question about listdir/regex/sort

Hi all. I'm kind of a python noobie, this is for my first usefull program, beyond just playing around with the language. Any help is appreciated. Here's the code that throws an error.

Expand|Select|Wrap|Line Numbers
  1. import re, os
  2.  
  3. files = os.listdir(os.getcwd())
  4. #files = ['blue13', 'red011', 'yellow1', 'green1000']
  5.  
  6. def getnum(filename): return float(re.findall(r'\d+',filename)[0])
  7. def numsort(a,b): return cmp(getnum(a),getnum(b))
  8.  
  9. files.sort(numsort)
  10. print files
this throws me
Expand|Select|Wrap|Line Numbers
  1. IndexError: list index out of range
if I switch the "files = " line for the commented one, everything works as it should

I've been banging my head on the keyboard for a couple hours now. It may be simple but I just don't see what's wrong.

Anyway... thanks for the help.
Jun 14 '07 #1
4 3488
bvdet
2,851 Expert Mod 2GB
Hi all. I'm kind of a python noobie, this is for my first usefull program, beyond just playing around with the language. Any help is appreciated. Here's the code that throws an error.

Expand|Select|Wrap|Line Numbers
  1. import re, os
  2.  
  3. files = os.listdir(os.getcwd())
  4. #files = ['blue13', 'red011', 'yellow1', 'green1000']
  5.  
  6. def getnum(filename): return float(re.findall(r'\d+',filename)[0])
  7. def numsort(a,b): return cmp(getnum(a),getnum(b))
  8.  
  9. files.sort(numsort)
  10. print files
this throws me
Expand|Select|Wrap|Line Numbers
  1. IndexError: list index out of range
if I switch the "files = " line for the commented one, everything works as it should

I've been banging my head on the keyboard for a couple hours now. It may be simple but I just don't see what's wrong.

Anyway... thanks for the help.
You are encountering files that have no numbers in them. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> getnum('split300.py')
  2. 300.0
  3. >>> getnum('xmlscanner.py')
  4. Traceback (most recent call last):
  5.   File "<interactive input>", line 1, in ?
  6.   File "C:\SDS2_7.0\macro\Work In Progress\re_files_with_numbers.py", line 7, in getnum
  7.     return float(re.findall(r'\d+',filename)[0])
  8. IndexError: list index out of range
  9. >>> 
Jun 14 '07 #2
bartonc
6,596 Expert 4TB
Hi all. I'm kind of a python noobie, this is for my first usefull program, beyond just playing around with the language. Any help is appreciated. Here's the code that throws an error.

Expand|Select|Wrap|Line Numbers
  1. import re, os
  2.  
  3. files = os.listdir(os.getcwd())
  4. #files = ['blue13', 'red011', 'yellow1', 'green1000']
  5.  
  6. def getnum(filename): return float(re.findall(r'\d+',filename)[0])
  7. def numsort(a,b): return cmp(getnum(a),getnum(b))
  8.  
  9. files.sort(numsort)
  10. print files
this throws me
Expand|Select|Wrap|Line Numbers
  1. IndexError: list index out of range
if I switch the "files = " line for the commented one, everything works as it should

I've been banging my head on the keyboard for a couple hours now. It may be simple but I just don't see what's wrong.

Anyway... thanks for the help.
And, just as a tip: Python allows the style of function def that you have use, but please don't do it. You will like yourself, and your program much better in the long run (because you will be able to read it more easily) if you stick with indents:
Expand|Select|Wrap|Line Numbers
  1. import re, os
  2.  
  3. files = os.listdir(os.getcwd())
  4. #files = ['blue13', 'red011', 'yellow1', 'green1000']
  5.  
  6. def getnum(filename):
  7.     return float(re.findall(r'\d+',filename)[0])
  8. def numsort(a,b):
  9.     return cmp(getnum(a),getnum(b))
  10.  
  11. files.sort(numsort)
  12. print files
For example, at first glance I couldn't see these functions.
Jun 14 '07 #3
bvdet
2,851 Expert Mod 2GB
Try this:
Expand|Select|Wrap|Line Numbers
  1. def getnum(filename):
  2.     m = re.search(r'\d+',filename)
  3.     if m: return int(m.group(0))
  4.     return None
Jun 14 '07 #4
Wow! thanks you guys for the quick response. That took care of it, it's up and running like it should.

Just for reference, here's what it looks like now.

Expand|Select|Wrap|Line Numbers
  1. import re,os
  2.  
  3. files = os.listdir(os.getcwd())
  4. #files = ['blue13', 'red011', 'yellow', 'green1000']
  5.  
  6. def numsort(c,d):
  7.         def getnum(filename):
  8.                 m = re.search(r'\d+',filename)
  9.                 if m: return float(m.group(0))
  10.                 return None
  11.         return cmp(getnum(c),getnum(d))
  12.  
  13. files.sort(numsort)
  14. print files
Thanks again! I'll likely be back!
Jun 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Jason Kratz | last post: by
8 posts views Thread by Hannu Kankaanp?? | last post: by
14 posts views Thread by Reinhold Birkenfeld | last post: by
4 posts views Thread by Python Dunce | last post: by
15 posts views Thread by Riccardo Galli | last post: by
1 post views Thread by kai | last post: by
3 posts views Thread by =?Utf-8?B?bWFnZ2ll?= | last post: by
4 posts views Thread by tonywh00t | last post: by
reply views Thread by Ronak mishra | last post: by

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.