LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2003, 12:04 AM   #1
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
more python fun!!


Hello.

Having some difficulties with a script. Here's the relevant part:
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 at the 'if sys.argv[1] == '-dir': ' line, but everything else works as it should.

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?
 
Old 05-16-2003, 02:04 AM   #2
jfabiani
Member
 
Registered: Apr 2003
Location: Woodland, CA
Distribution: RH 8.0
Posts: 33

Rep: Reputation: 15
if len(args) < 1:
raise Exception, "Wrong number of arguments"
 
Old 05-16-2003, 12:50 PM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
I'm afraid that didn't work. Here's the traceback:

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

As you can see it is completely ignoring my else: clause, which is supposed to be the catch all right? It seems to check the first test: (if sys.argv[1] == '-dir' then croak because the variable being checked doesn't exist. I thought it was just supposed to move down to the next test if the first doesn't apply.

Anybody have ideas?
 
Old 05-16-2003, 12:58 PM   #4
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
Ok I got it:

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])
I just had to test for the missing variable first...

Thanks for the help jfabiani
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 07:02 PM
installing python library's (Python Numeric) Four Linux - Newbie 1 10-16-2005 02:31 PM
WineX, python-gnome, and python-gtk DrD Fedora 0 08-03-2004 12:11 PM
install python 2.3 ,necssary to remove python 2.2 ngan_yine Linux - Newbie 7 12-28-2003 04:07 PM
Try Python, O'reilly Learning Python haknot Programming 5 02-15-2002 08:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration