tags:

views:

4741

answers:

4
if len(sys.argv) < 2:
    sys.stderr.write('Usage: sys.argv[0] ')
    sys.exit(1)


if not os.path.exists(sys.argv[1]):
    sys.stderr.write('ERROR: Database sys.argv[1] was not found!')
    sys.exit(1)

This is a portion of code I'm working on. The first part I'm trying to say if the user doesn't type.... python programname something then it will exit.

The second part I'm trying to see if the database exists. On both places I'm unsure if I have the correct way to write out the sys.argv's by stderr or not.

Any other ideas are welcome! Thanks

+12  A: 

In Python, you can't just embed arbitrary Python expressions into literal strings and have it substitute the value of the string. You need to either:

sys.stderr.write("Usage: " + sys.argv[0])

or

sys.stderr.write("Usage: %s" % sys.argv[0])

Also, you may want to consider using the following syntax of print (for Python earlier than 3.x):

print >>sys.stderr, "Usage:", sys.argv[0]

Using print arguably makes the code easier to read. Python automatically adds a space between arguments to the print statement, so there will be one space after the colon in the above example.

In Python 3.x, you would use the print function:

print("Usage:", sys.argv[0], file=sys.stderr)

Finally, in Python 2.6 and later you can use .format:

print >>sys.stderr, "Usage: {0}".format(sys.argv[0])
Greg Hewgill
Note that 'Usage: {0}'.format(sys.argv[0]) is now the recommended way of formatting strings.
Bastien Léonard
Thanks, added that.
Greg Hewgill
Also note that sys.stderr.write() will not add a trailing newline, so you should include one explicitly.
Marius Gedminas
+2  A: 

In your first if-block doing print 'Usage:' + sys.argv[0] does not tell the user that they need to type "python [program_name] [something]". It only prints "Usage: [program_name]". You might want to come up with a better error message.

ntownsend
+3  A: 

I would do it this way:

import sys

def main(argv):
    if len(argv) < 2:
        sys.stderr.write("Usage: %s <database>" % (argv[0],))
        return 1

    if not os.path.exists(argv[1]):
        sys.stderr.write("ERROR: Database %r was not found!" % (argv[1],))
        return 1

if __name__ == "__main__":
    sys.exit(main(sys.argv))

This allows main() to be imported into other modules if desired, and simplifies debugging because you can choose what argv should be.

John Millikin
This is a nice explanation - I was looking for more information on using sys.exit( main(sys.argv) ) and why it was encouraged. Thanks for the explanation.
Helen Neely
+5  A: 

BTW you can pass the error message directly to sys.exit:

if len(sys.argv) < 2:
    sys.exit('Usage: %s database-name' % sys.argv[0])

if not os.path.exists(sys.argv[1]):
    sys.exit('ERROR: Database %s was not found!' % sys.argv[1])
Marius Gedminas