473,390 Members | 1,034 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,390 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 3688
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

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

Similar topics

11
by: Jason Kratz | last post by:
OK. I've search on google groups and around the web for this and I haven't found an answer. I'm a Python newbie and have what I assume is a basic question. os.listdir takes a pathname as an arg...
8
by: Hannu Kankaanp?? | last post by:
This may be a bug or simply a strange result of undefined behaviour, but this is what I get with Python 2.3.2 on Windows XP: >>> import os >>> os.listdir('') >>> os.listdir(u'')
14
by: Reinhold Birkenfeld | last post by:
Hello, I recently ported a simple utility script to analyze a data file from Perl to Python that uses regex substitutions, not more complex than re1 = re.compile(r"\s*<.*>\s*") re2 =...
4
by: Python Dunce | last post by:
I've run into an issue with glob and matching filenames with brackets '' in them. The problem comes when I'm using part of such a filename as the path I'm passing to glob. Here's a trimmed down...
15
by: Riccardo Galli | last post by:
Hi, I noticed that when I use os.listdir I need to work with absolute paths 90% of times. While I can use a for cycle, I'd prefere to use a list comprehension, but it becomes too long. I...
2
by: bebop | last post by:
Hi, I have a Regular Expression that will match the format a user provides in a textbox. Ex. User types Brown,Joe in the textbox I would like the expression to have both whitespace and...
1
by: kai | last post by:
Hello, I use dircache.listdir(myDir) in my module repeatedly. On OS WIN 2000 listdir() will re-read the directory structure! But on AIX, listdir() will not re-read the directory structure (see...
3
by: =?Utf-8?B?bWFnZ2ll?= | last post by:
hi, I've been working getting a file parsed out using Regex. There's something I don't understand. When I define the pattern for my fields in my file, I am telling regex to grab those fields (...
4
by: tonywh00t | last post by:
Hi everyone, I have a "simple" question, especially for people familiar with regex. I need to parse strings that have the form: 1:3::5:9 which indicates the set of integers {1 3 4 5 9}. In...
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: 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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.