Question about listdir/regex/sort

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • quailman
    New Member
    • Jun 2007
    • 2

    #1

    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.

    Code:
    import re, os
    
    files = os.listdir(os.getcwd())
    #files = ['blue13', 'red011', 'yellow1', 'green1000']
    
    def getnum(filename): return float(re.findall(r'\d+',filename)[0])
    def numsort(a,b): return cmp(getnum(a),getnum(b))
    
    files.sort(numsort)
    print files
    this throws me
    Code:
    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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by quailman
    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.

    Code:
    import re, os
    
    files = os.listdir(os.getcwd())
    #files = ['blue13', 'red011', 'yellow1', 'green1000']
    
    def getnum(filename): return float(re.findall(r'\d+',filename)[0])
    def numsort(a,b): return cmp(getnum(a),getnum(b))
    
    files.sort(numsort)
    print files
    this throws me
    Code:
    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:
    Code:
    >>> getnum('split300.py')
    300.0
    >>> getnum('xmlscanner.py')
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
      File "C:\SDS2_7.0\macro\Work In Progress\re_files_with_numbers.py", line 7, in getnum
        return float(re.findall(r'\d+',filename)[0])
    IndexError: list index out of range
    >>>

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by quailman
      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.

      Code:
      import re, os
      
      files = os.listdir(os.getcwd())
      #files = ['blue13', 'red011', 'yellow1', 'green1000']
      
      def getnum(filename): return float(re.findall(r'\d+',filename)[0])
      def numsort(a,b): return cmp(getnum(a),getnum(b))
      
      files.sort(numsort)
      print files
      this throws me
      Code:
      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:
      Code:
      import re, os
      
      files = os.listdir(os.getcwd())
      #files = ['blue13', 'red011', 'yellow1', 'green1000']
      
      def getnum(filename):
          return float(re.findall(r'\d+',filename)[0])
      def numsort(a,b):
          return cmp(getnum(a),getnum(b))
      
      files.sort(numsort)
      print files
      For example, at first glance I couldn't see these functions.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Try this:[code=Python]def getnum(filename ):
        m = re.search(r'\d+ ',filename)
        if m: return int(m.group(0))
        return None[/code]

        Comment

        • quailman
          New Member
          • Jun 2007
          • 2

          #5
          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.

          Code:
          import re,os
          
          files = os.listdir(os.getcwd())
          #files = ['blue13', 'red011', 'yellow', 'green1000']
          
          def numsort(c,d):
                  def getnum(filename):
                          m = re.search(r'\d+',filename)
                          if m: return float(m.group(0))
                          return None
                  return cmp(getnum(c),getnum(d))
          
          files.sort(numsort)
          print files
          Thanks again! I'll likely be back!

          Comment

          Working...