python command line arguments in main, skip script name
The real answer is to learn about and use argparse
, though.
Since sys.argv is a list, you can use slicing sys.argv[1:]
:
def main(argv):
for x in argv[1:]:
build(x)
if __name__ == "__main__":
main(sys.argv)
But, if you can only have one script parameter, just get it by index: sys.argv[1]
. But, you should check if the length of sys.argv
is more than 1 and throw an error if it doesn't, for example:
def main(argv):
if len(argv) == 1:
print "Not enough arguments"
return
else:
build(argv[1])
if __name__ == "__main__":
main(sys.argv)