http://stackoverflow.com/questions/2626026/python-sys-argv-lists-and-indexes
1.
C:\> hello.py John
argv
represents all the items that come along via the command-line input, but counting starts at zero (0) not one (1): in this case, "hello.py
" is element 0, "John
" is element 1
in other words, sys.argv[0] == 'hello.py'
and sys.argv[1] == 'John'
... but look, how many elements is this? 2, right! so even though the numbers are 0 and 1, there are 2 elements here.
1.
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.
2.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])