#1
  1. No Profile Picture
    slacker
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Penticton BC Canada
    Posts
    35
    Rep Power
    13

    if: elif: else:...


    Hello all...


    Having problems with a script. Heres the relevant section:
    Code:
    if sys.argv[1] == '-dir':
    	convertDirectory(sys.argv[2])
    
    elif sys.argv[1] == '--help':
        showUsage()
    
    elif string.count(sys.argv[1], '.mp3') == 1:
        convertSong(sys.argv[1])
    
    else:
        showUsage()
    What I want is if no command line arguments are given, the usage details are shown. When I test it with no arguments however, I get an error telling me that sys.argv[1] is out of range, which is true: it doesn't exist, which is kinda the point.

    The traceback shows the error as:
    'Traceback (most recent call last):
    File "./dir2ogg.py", line 82, in ?
    if sys.argv[1] == '-dir':
    IndexError: list index out of range'

    I tried testing for sys.argv[1] ie: elif sys.argv[1] == 0: but always get the out of range error.

    What am I doing wrong here? It seems as though python is ignoring my catchall else: clause. When the other clauses are tested they work as they should.
    Support the mob or mysteriously disappear!!
  2. #2
  3. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    Try this (I can't test it at the mo, so it's off the top of my head)

    PHP Code:
    if not sys.argv[1]:
        
    showUsage()

    elif if sys.argv[1is '-dir':
        
    convertDirectory(sys.argv[2])

    elif sys.argv[1is '--help':
        
    showUsage()

    elif string.count(sys.argv[1], '.mp3'is 1:
        
    convertSong(sys.argv[1]) 
    You might also want to look at the module getopt, which is pretty handy in cases like this.
  4. #3
  5. No Profile Picture
    slacker
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Penticton BC Canada
    Posts
    35
    Rep Power
    13
    Thanks for the reply. It didn't work, but it got me thinking. I reordered the if, else's and tested for the length of sys.argv
    here's the code:
    Code:
    if len(sys.argv) < 2:
        showUsage()
    
    elif sys.argv[1] == '-dir':
            convertDirectory(sys.argv[2])
    
    elif sys.argv[1] == '--help':
        showUsage()
    
    elif string.count(sys.argv[1], '.mp3') == 1:
        convertSong(sys.argv[1])
    
    else:
        showUsage()
    Works perfect now...

    thanks for the help
    Support the mob or mysteriously disappear!!
  6. #4
  7. Kiss My Converse!
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    May 2003
    Location
    Davie, Florida
    Posts
    1,520
    Rep Power
    339
    Bulliver, did you get this to work?
  8. #5
  9. No Profile Picture
    slacker
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Penticton BC Canada
    Posts
    35
    Rep Power
    13
    Yes I did

    that last bit of code I posted did the trick...
  10. #6
  11. Kiss My Converse!
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    May 2003
    Location
    Davie, Florida
    Posts
    1,520
    Rep Power
    339
    Excellent - thanks for the update!

IMN logo majestic logo threadwatch logo seochat tools logo