Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 16th, 2003, 12:38 PM
bulliver bulliver is offline
slacker
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Penticton BC Canada
Posts: 35 bulliver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to bulliver
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!!

Reply With Quote
  #2  
Old May 17th, 2003, 08:58 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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.

Reply With Quote
  #3  
Old May 17th, 2003, 02:25 PM
bulliver bulliver is offline
slacker
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Penticton BC Canada
Posts: 35 bulliver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to bulliver
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

Reply With Quote
  #4  
Old May 22nd, 2003, 09:35 AM
Jcaputo's Avatar
Jcaputo Jcaputo is offline
Kiss My Converse!
Developer Shed Admin.
 
Join Date: May 2003
Location: Davie, Florida
Posts: 1,517 Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 17 h 30 m 24 sec
Reputation Power: 10
Bulliver, did you get this to work?

Reply With Quote
  #5  
Old May 22nd, 2003, 02:08 PM
bulliver bulliver is offline
slacker
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Penticton BC Canada
Posts: 35 bulliver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to bulliver
Yes I did

that last bit of code I posted did the trick...

Reply With Quote
  #6  
Old May 22nd, 2003, 02:24 PM
Jcaputo's Avatar
Jcaputo Jcaputo is offline
Kiss My Converse!
Developer Shed Admin.
 
Join Date: May 2003
Location: Davie, Florida
Posts: 1,517 Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level)Jcaputo User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 17 h 30 m 24 sec
Reputation Power: 10
Excellent - thanks for the update!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > if: elif: else:...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway