By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,423 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

Question about listdir/regex/sort

P: 2
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
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
Expert 5K+
P: 6,596
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
Expert Mod 2.5K+
P: 2,851
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

P: 2
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.