60
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.

Serenity
  • 32,301
  • 18
  • 107
  • 109
Tyler
  • 3,779
  • 7
  • 26
  • 25
  • 3
    This should be called "python and sys.stderr", tho it actually helped me on the `argv` :P – cregox Apr 13 '10 at 21:49
  • Exact same thing happened to me, @Cawas – Stedy Apr 28 '10 at 23:25
  • 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 Jun 11 '09 at 19:57

3 Answers3

99

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
  • 10,676
  • 3
  • 38
  • 38
34

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
  • 890,778
  • 177
  • 1,125
  • 1,260
32

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
  • 191,086
  • 39
  • 207
  • 222
  • 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 Feb 28 '10 at 22:46