BurnTard has got a new IndexError

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BurnTard
    New Member
    • May 2007
    • 52

    BurnTard has got a new IndexError

    Okay, so I know what an IndexError is and all, but that doesn't help me understand why on earth I'm getting this one. I'm writing a program that's supposed skim through directories, dump all files into one list and all directories into another. That's as far as I've gotten now because this is where the nasty error shows up at the very beginning. So without further ado, here's my script:

    [code=python]

    import os
    tralla='/windows'
    tintin='/LOLOL'
    list1=[]
    list2=[]

    for item in os.listdir(tral la):
    if os.path.splitex t(item)[1][0]=='.':
    list1.append(it em)
    else:
    list2.append(it em)


    for item in os.listdir(tint in):
    if os.path.splitex t(item)[1][0]=='.':
    list1.append(it em)
    else:
    list2.append(it em)

    for item in list2:
    print item

    [/code]

    So can anyone tell me why on earth I get this:
    "Traceback (most recent call last):
    File "untitled.p y", line 8, in <module>
    if os.path.splitex t(item)[1][0]=='.':
    IndexError: string index out of range"
    ?

    At least this time it's with tab-indents, some people didn't like it when I used spaces last time... :)
    Any help I can get is appreciated.
  • dazzler
    New Member
    • Nov 2007
    • 75

    #2
    what does "print os.path.splitex t(item)" look like?

    Comment

    • dazzler
      New Member
      • Nov 2007
      • 75

      #3
      hmmh, just tried this and it worked for me

      oh no it didn't...

      Comment

      • dazzler
        New Member
        • Nov 2007
        • 75

        #4
        ok, if I have file called "001png"
        and print os.path.splitex t(item) prints ('001png', '')
        where [0] is '001png'
        and [1] = '' ,
        [1][0] would be the first character in [1] but [1] is empty so [1][0] doesn't even exist

        instead of
        [code=python]
        if os.path.splitex t(item)[1][0] =='.'
        [/code]

        use
        [code=python]
        if os.path.splitex t(item)[1] !='' #if [1] is anything else than empty
        [/code]

        :)

        Comment

        • BurnTard
          New Member
          • May 2007
          • 52

          #5
          Originally posted by dazzler
          ok, if I have file called "001png"
          and print os.path.splitex t(item) prints ('001png', '')
          where [0] is '001png'
          and [1] = '' ,
          [1][0] would be the first character in [1] but [1] is empty so [1][0] doesn't even exist

          instead of
          [code=python]
          if os.path.splitex t(item)[1][0] =='.'
          [/code]

          use
          [code=python]
          if os.path.splitex t(item)[1] !='' #if [1] is anything else than empty
          [/code]

          :)
          Anku! (mulefa for thank you!)
          I wonder why I didn't think of that, that is brilliant!

          Comment

          Working...