|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Try this (I can't test it at the mo, so it's off the top of my head)
PHP Code:
You might also want to look at the module getopt, which is pretty handy in cases like this. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
Bulliver, did you get this to work?
|
|
#5
|
|||
|
|||
|
Yes I did
![]() that last bit of code I posted did the trick... |
|
#6
|
||||
|
||||
|
Excellent - thanks for the update!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > if: elif: else:... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|